自定义EditText垂直对齐方式
我创建了一个自定义 EditText (实际上,我只是设置了一个背景可绘制)。问题是它的文本总是顶部对齐,而我希望它垂直居中对齐。我已经尝试将其重力设置为 CENTER_VERTICAL,但它不起作用。这是我创建的可绘制对象:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:gravity="center_vertical">
<solid android:color="#FFFFFF"/>
<stroke android:width="1dp" android:color="#88BA52" />
<corners
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>
</shape>
以下是我创建它的方法:
searchEditText = new EditText(getContext());
searchEditText.setTextSize(12);
searchEditText.setSingleLine();
searchEditText.setGravity(Gravity.CENTER_VERTICAL);
searchEditText.setHint(R.string.search_hint);
searchEditText.setBackgroundDrawable(
getResources().getDrawable(R.drawable.search_field));
addView(searchEditText);
I created a custom EditText (actually, I just set a background drawable). The problem is that its text is always top-aligned, and I want it to be vertical-center-aligned. I've already tried to set its gravity to CENTER_VERTICAL, but it doesn't work. This is the drawable I created:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:gravity="center_vertical">
<solid android:color="#FFFFFF"/>
<stroke android:width="1dp" android:color="#88BA52" />
<corners
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>
</shape>
And here's how I create it:
searchEditText = new EditText(getContext());
searchEditText.setTextSize(12);
searchEditText.setSingleLine();
searchEditText.setGravity(Gravity.CENTER_VERTICAL);
searchEditText.setHint(R.string.search_hint);
searchEditText.setBackgroundDrawable(
getResources().getDrawable(R.drawable.search_field));
addView(searchEditText);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可能有帮助
This might help
尝试减小字体大小。无论出于何种原因,我发现如果您的字体大小太大,即使您认为 EditText 应该足够高以容纳文本,文本也会显得比正确垂直居中更高。
向下调整字体大小,或者如果使用默认值则指定字体大小,直到找到足够小的字体大小值以使其显示正确居中。
Try reducing your font size. For whatever reasons, I have found if your font size is too large, even if it appears to you the EditText should be tall enough to hold the text, the text will appear to be higher than properly centered vertically.
Adjust your font size down, or specify a font size if you are using default value, until you find a font size value small enough for it to appear properly centered.
我看到同样的问题。以编程方式创建 EditText 导致重力被垂直强制到顶部(而水平居中仍然有效)......直到我注释掉设置背景的调用!在这种情况下,垂直和水平重力都受到尊重。
这是我的代码:
现在,操作系统源代码中的步骤跟踪表明,垂直居中的计算奇怪地依赖于背景报告的高度。 EditText 中的默认值是
NinePatchDrawable
,具有 64 像素高的默认位图。如果您的 EditText 是这个高度,您的文本将居中。否则,它会比应有的更接近顶部。设置背景颜色将在内部使用ColorDrawable
报告固有高度为零,因此仅使用文本高度并将其垂直对齐到顶部。解决这个问题的方法是创建自己的
Drawable
子类,将其设置为EditText实例的背景,确保调用setBounds() 在可绘制对象上,以便它有一个要报告的高度,并重写可绘制对象的
getIntrinsicHeight()
方法以报告使用setBounds()
设置的高度。I'm seeing the same issue. Programmatically creating an EditText resulted in the gravity being vertically forced to TOP (while horizontal centering still works) ... until I commented out the calls that set the background! In this case, vertical and horizontal gravity are both honored.
Here is my code:
Now, step tracing in the OS source code reveals that the computation for vertical centering strangely relies on the height reported by the background. Defaut in EditText is a
NinePatchDrawable
with a 64 pixels high default bitmap. If your EditText is this height, your text will be centered. Otherwise, it will be closer to top than it should. Setting a background color will internally use aColorDrawable
which reports an intrinsic height of zero, therefore only using the text height and vertically aligning it to TOP.The way to fix this issue is to create your own
Drawable
subclass, set it as the background of the EditText instance, make sure you callsetBounds()
on the drawable so it has a height to report, and override thegetIntrinsicHeight()
method of the drawable to report the height that was set usingsetBounds()
.