Android 将ImageView与EditText水平对齐

Android 将ImageView与EditText水平对齐,android,imageview,android-edittext,alignment,Android,Imageview,Android Edittext,Alignment,我正在努力寻找一种在Android上正确对齐编辑文本和图像视图的方法。我一直得到这样的结果: XML部分非常简单: <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/image"

我正在努力寻找一种在Android上正确对齐
编辑文本
图像视图
的方法。我一直得到这样的结果:

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的(RelativeLayout与alignTop/alignBottom):


但结果完全一样

我尝试过使用EditText的背景填充、固有高度,但没有效果

EditText的背景可绘制性在Android版本/ROM中有所不同,我想支持所有版本


如何在任何android版本(和样式)上实现像素完美对齐?

使用
android:layout\u centerInParent=true
编辑文本并查看其是否有效?

您需要将
RelativeLayout
android:align\u bottom
android:align\u Top
属性一起使用。但是我没有编写代码。

删除editText和ImageView的属性android:gravity=“center”,并将linearlayout的gravity属性设置为垂直居中。

您是否尝试过
editText的
setCompoundDrawables()
方法

您可以尝试使用Drawable上的
setBounds()
方法或使用
setCompoundDrawablesWithInstrinsicBounds()
方法设置内部边界


这应该适用于所有设备。

最终找到了一个适合的解决方案,可以跨不同的Android版本/ROM/风格进行扩展

主要问题是EditText的背景绘图本身具有透明的填充:

此外,我还注意到,在不同的Android版本/ROM/样式之间,这种透明填充的差别很大(例如,库存IC根本没有透明填充)

中间结论是,我的原始代码正确地将ImageView与EditText对齐。但是,我真正想要的是将ImageView与EditText背景的可见部分对齐

为了实现这一点,我扫描了从EditText的背景drawable创建的位图。我从上到下和从下到上扫描它,以找到完全透明的线的数量,并使用这些值作为ImageView的上/下填充。所有这些在我的N1上持续花费不到5毫秒。代码如下:

if(editor.getBackground() != null) {
    int width = editor.getWidth();
    int height = editor.getHeight();
    Drawable backgroundDrawable = editor.getBackground().getCurrent();

    // Paint the editor's background in our bitmap
    Bitmap tempBitmap =  Bitmap.createBitmap(width, height, Config.ARGB_4444);
    backgroundDrawable.draw(new Canvas(tempBitmap));

    // Get the amount of transparent lines at the top and bottom of the bitmap to use as padding
    int topPadding = countTransparentHorizontalLines(0, height, tempBitmap);
    int bottomPadding = countTransparentHorizontalLines(height-1, -1, tempBitmap);

    // Discard the calculated padding if it means hiding the image
    if(topPadding + bottomPadding > height) {
        topPadding = 0;
        bottomPadding = 0;
    }

    tempBitmap.recycle();

    // Apply the padding
    image.setPadding(0, topPadding, 0, bottomPadding);
}

private int countTransparentHorizontalLines(int fromHeight, int toHeight, Bitmap bitmap) {
    int transparentHorizontalLines = 0;
    int width = bitmap.getWidth();
    int currentPixels[] = new int[width];
    boolean fullyTransparentLine = true;

    boolean heightRising = (fromHeight < toHeight);
    int inc =  heightRising ? 1 : -1;

    for(int height = fromHeight; heightRising ? (height < toHeight) : (toHeight < height); height+=inc) {
        bitmap.getPixels(currentPixels, 0, width, 0, height, width, 1);

        for(int currentPixel : currentPixels) {
            if(currentPixel != Color.TRANSPARENT && Color.alpha(currentPixel) != 255) {
                fullyTransparentLine = false;
                break;
            }
        }

        if(fullyTransparentLine)
            transparentHorizontalLines++;
        else
            break;
    }

    return transparentHorizontalLines;
}
if(editor.getBackground()!=null){
int width=editor.getWidth();
int height=editor.getHeight();
Drawable backgroundDrawable=editor.getBackground().getCurrent();
//在位图中绘制编辑器的背景
Bitmap tempBitmap=Bitmap.createBitmap(宽度、高度、配置ARGB_4444);
背景可绘制。绘制(新画布(tempBitmap));
//获取位图顶部和底部用作填充的透明线的数量
int-topPadding=counttransparenthorzontallines(0,高度,tempBitmap);
int bottomPadding=counttransparenthorzontallines(高度-1,-1,tempBitmap);
//如果计算的填充意味着隐藏图像,则放弃该填充
如果(顶部添加+底部填充>高度){
加总=0;
底部填充=0;
}
tempBitmap.recycle();
//涂上填充物
setPadding(0,topPadding,0,bottomPadding);
}
私有int countTransparentAuthorizontallines(int-fromHeight、int-toHeight、位图){
int TransparentAuthorizontallines=0;
int width=bitmap.getWidth();
int currentPixels[]=新的int[宽度];
布尔fullyTransparentLine=true;
布尔高度上升=(从高度<到高度);
int inc=高度上升?1:-1;
对于(内部高度=从高度;高度上升)(高度<到高度):(到高度<高度);高度+=inc){
getPixels(当前像素,0,宽度,0,高度,宽度,1);
用于(int currentPixel:currentPixels){
if(currentPixel!=Color.TRANSPARENT&&Color.alpha(currentPixel)!=255){
fullyTransparentLine=假;
打破
}
}
if(完全透明线)
透明线++;
其他的
打破
}
返回透明线;
}
它就像一个符咒


要去掉编辑文本中的填充,只需使用自定义图像作为背景。。。有阴影的矩形就足够了。。。或者随便你喜欢什么。。。并使用为EditText指定背景

android:background="@drawable/myBackground"

我使用了一种快速解决方法,只需将EditText向下移动2dp,只需将以下内容添加到EditText:

android:layout_marginBottom="-2dp"
android:layout_marginTop="2dp"

你应该根据你的屏幕分辨率分配编辑文本和图像的版面权重,通过分配版面权重,它在任何横向或纵向模式下都会看起来很完美。试试看,它可能会起作用。使用填充可能会在不同分辨率下导致不可预测的结果。不幸的是,没有。而且,
android:layout\u centerInParent
对LinearLayout没有影响。它的LinearLayout so
android:layout\u centerInParent
将不起作用。这在分辨率/OS版本/ROM之间无法很好地扩展。您不需要:)无论如何,它不起作用。我将它们包装在一个RelativeLayout中,并在ImageView中添加了
android:layout\u alignTop
android:layout\u alignBottom
。结果与上面的屏幕截图相同。您可以粘贴吗,您是如何实现的?您还可以在ImageView和TextView中添加
android:layout\u alignParentTop=“true”
android:layout\u alignParentBottom=“true”
。在这种情况下,您可以为编辑文本添加透明背景。不过,让我试一下。我刚刚测试了它,你是对的,有内在的裕度导致了这个问题。删除它的唯一方法是在EditText中使用android:background=“#FFFF0000”
。你可以选择任何颜色,但它不会是默认的样式。我会尝试并报告结果。谢谢我试过了,效果很好。然而,这并非如此
android:layout_marginBottom="-2dp"
android:layout_marginTop="2dp"