如何使android EditText的高度小于默认值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1361332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make android EditText smaller than default in height?
提问by Hymany
How to make android EditText smaller than default in height?
如何使android EditText的高度小于默认值?
If I change the height, I can't see my text, but it has extra blank on bottom.
如果我改变高度,我看不到我的文字,但底部有额外的空白。
回答by bhatt4982
EditText has a default padding set.
EditText 有一个默认的填充集。
Try this,
尝试这个,
<EditText
android:text="Any Text"
android:layout_width="fill_parent"
android:layout_height="16dp"
android:padding="2dp"
android:background="#FFF"
android:textSize="10dp"
/>
回答by learn_andrd
Use
用
android:textAppearance="?android:attr/textAppearanceSmallInverse"
For further reference, see this : http://developer.android.com/guide/topics/ui/themes.html
如需进一步参考,请参阅:http: //developer.android.com/guide/topics/ui/themes.html
回答by Timo
I wasn't really satisfied with any of the answers there though when combined they mostly contain all the ingredients to the right solution. But let's first identify what is the problem. The problem is as bhatt4982 stated the padding and he has provided a solution to that. Set padding to what ever you want. If you also want a smaller font do something like learn_andrd suggests or something like this
我对那里的任何答案都不满意,尽管将它们结合起来,它们主要包含正确解决方案的所有成分。但让我们首先确定问题是什么。问题在于 bhatt4982 说明了填充,他为此提供了解决方案。将填充设置为您想要的任何内容。如果您还想要较小的字体,请执行诸如 learn_andrd 建议之类的操作
style="@android:style/TextAppearance.Small.Inverse"
Now, the width of the EditText element is right and the font size matches that of your regular TextView. The color is however inverse as light grey don't look too good on white background. And that brings us to the last of our problems, the background. You need to set a different background because the default EditText background has a minimum height. That height is the height of the current height of the element. AFAIK Android doesn't support scaling down background images. You should define your own background Drawable for the EditText that allows heights smaller than what the default one allows. Otherwise it will have no effect what so ever no matter what you try to set for the height or padding or maring.
现在,EditText 元素的宽度是正确的,并且字体大小与常规 TextView 的字体大小相匹配。然而,颜色是相反的,因为浅灰色在白色背景上看起来不太好。这将我们带到了我们的最后一个问题,即背景。您需要设置不同的背景,因为默认的 EditText 背景具有最小高度。该高度是元素当前高度的高度。AFAIK Android 不支持缩小背景图像。你应该为 EditText 定义你自己的背景 Drawable,它允许的高度小于默认允许的高度。否则,无论您尝试为高度、填充或母线设置什么,它都不会产生任何影响。
P.S. You might wonder why they have made it so difficult for you to create smaller EditViews... perhaps it has something to do with the fact that, even as it is, it is very difficult to use your finger to move the cursor to a specific position in the EditText (say for fixing a typo without erasing everything) element. It will become impossible if you make the text even smaller. Thus, you should probably reconsider is it, what your trying to achieve, really something worth pursuing unless your just trying to cut down on the padding.
PS 你可能想知道为什么他们让你创建更小的 EditViews 如此困难......也许这与这样一个事实有关,即使是这样,用手指将光标移动到一个EditText 中的特定位置(比如在不删除所有内容的情况下修复拼写错误)元素。如果您将文本变得更小,这将变得不可能。因此,您可能应该重新考虑一下,您想要实现的目标,真的值得追求,除非您只是想减少填充。
回答by Dimitar Dimitrov
Is it an option to explicitly set the height?
是否可以选择明确设置高度?
If yes, use something like
如果是,请使用类似的东西
<EditText
android:id="@+id/smallEditText"
android:layout_width="wrap_content"
android:layout_height="10dip"/>
You can decrease the font size, by using android:textSize
in XML. You can also make your control scrollable.
您可以通过android:textSize
在 XML 中使用来减小字体大小。您还可以使控件可滚动。
Edit: Just saw your screenshots, what is the layout in which you have put the EditText? It may be setting non-explicit layout rules for your control.
编辑:刚刚看到你的截图,你把 EditText 放在什么布局中?它可能正在为您的控件设置非显式布局规则。
回答by Adiii
change the XML edit text these two property
更改 XML 编辑文本这两个属性
android:layout_height="15dp"
android:textSize="12sp"