将 ImageView 与 EditText 水平对齐
我正在努力寻找一种在 Android 上EditText
和 ImageView
正确 对齐的方法。我不断得到这个结果:
XML 部分非常简单:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:scaleType="centerInside"
android:src="@drawable/android"
android:visibility="gone" />
<EditText
android:id="@+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true" />
</LinearLayout>
我也尝试了许多建议下面,包括 PravinCG 的(具有alignTop/alignBottom 的RelativeLayout):
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/edittext"
android:layout_alignTop="@+id/edittext"
android:scaleType="centerInside"
android:src="@drawable/android"
android:visibility="visible" />
<EditText
android:id="@+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/image"
android:hint="@string/username"
android:singleLine="true" />
</RelativeLayout>
但结果是完全相同的。
我尝试过使用 EditText 的背景填充、固有高度,但无济于事。
EditText 的背景绘制在 Android 版本/ROM 之间有所不同,我想支持它们。
如何在任何 Android 版本(和样式)上实现像素完美对齐?
I'm trying hard to find a way of aligning an EditText
and an ImageView
properly on Android. I keep getting this result:
The XML part is quite simple:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:scaleType="centerInside"
android:src="@drawable/android"
android:visibility="gone" />
<EditText
android:id="@+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true" />
</LinearLayout>
I've also tried many of the suggestions below, including PravinCG's (RelativeLayout with alignTop/alignBottom):
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/edittext"
android:layout_alignTop="@+id/edittext"
android:scaleType="centerInside"
android:src="@drawable/android"
android:visibility="visible" />
<EditText
android:id="@+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/image"
android:hint="@string/username"
android:singleLine="true" />
</RelativeLayout>
But the result is exactly the same.
I've tried playing with the EditText's background padding, intrinsic height, but to no avail.
The EditText's background drawable is different among Android versions/ROMs, and I want to support them all.
How can I make this alignment pixel perfect on any android version (and style)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
最终找到了一个合适的解决方案,可以跨不同的 Android 版本/ROM/风格进行扩展。
主要问题是 EditText 的背景可绘制本身具有透明填充:

另外,我注意到这种透明填充在不同的 Android 之间存在很大差异版本/ROM/样式(例如,库存 ICS 根本没有透明填充)。
总而言之,我的原始代码将 ImageView 与 EditText 正确对齐。然而,我真正想要的是将我的 ImageView 与 EditText 背景的可见部分对齐。
为了实现这一点,我扫描了从 EditText 的背景可绘制对象创建的位图。我从上到下和从下到上扫描它以找到完全透明线的数量,并将这些值用作我的 ImageView 的顶部/底部填充。在我的 N1 上,所有这一切持续花费的时间不到 5 毫秒。代码如下:
它的作用就像一个魅力!
Finally found a suitable solution that scales across different Android versions/ROMs/styles.
The main problem is that the EditText's background drawable itself has transparent padding:

Also, I've noticed this transparent padding varies a lot between different Android versions/ROMs/styles (stock ICS, for instance, has no transparent padding at all).
In a mid-way conclusion, my original code properly alignes my ImageView with my EditText. However, what I really want is to align my ImageView with the visible part of the EditText's background.
To achieve this, I scan a Bitmap I create from my EditText's background drawable. I scan it top-bottom and bottom-up to find the amount of fully transparent lines and use those values as top/bottom padding for my ImageView. All this consistently takes less than 5ms on my N1. Here's the code:
And it works like a charm!
您需要将
RelativeLayout
与android:align_bottom
和android:align_Top
属性一起使用。不过我不是在写代码。You need to use
RelativeLayout
withandroid:align_bottom
andandroid:align_Top
attribute. I am not writing the code though.您是否尝试过
EditText
的setCompoundDrawables()
方法。您可以尝试使用 Drawable 上的
setBounds()
方法或使用setCompoundDrawablesWithInstrinsicBounds()
方法来设置内在边界。这应该适用于所有设备。
Have you tried the
setCompoundDrawables()
methods for theEditText
.You can try setting the intrinsic bounds using the
setBounds()
method on the Drawable or by using thesetCompoundDrawablesWithInstrinsicBounds()
method.This should work on all devices.
要摆脱 EditText 中的填充,只需使用自定义图像作为背景...带有一些阴影的矩形就足够了...或任何您喜欢的...并将背景分配给 EditText 使用
To get rid of the padding in the EditText, just use a custom image as background... rectangle with some shadow is enough... or whatever you like... and assign the background to the EditText using
使用
android:layout_centerInParent = true
编辑文本并查看它是否有效?use
android:layout_centerInParent = true
for edit text and see weather it works?删除editText和ImageView的属性android:gravity =“center”并将线性布局的重力属性设置为center_vertically。
remove editText's and ImageView's properties android:gravity="center" and set gravity property of linearlayout to center_vertically.
我使用了一种快速解决方法,只需将以下内容添加到 EditText,即可将 EditText 向下移动 2dp:
I used a quick workaround by simply moving the EditText down 2dp by simply adding the following to the EditText: