android - 水平对齐 ImageView 和 EditText

标签 android imageview android-edittext alignment

我正在努力寻找一种在 Android 上EditTextImageView 正确 对齐的方法。我一直得到这个结果:

enter image description here

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 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>

但结果完全一样。

我试过使用 EditText 的背景填充、固有高度,但无济于事。

EditText 的背景 drawable 在不同的 Android 版本/ROM 中是不同的,我想支持它们。

如何在任何 Android 版本(和样式)上使此对齐像素完美

最佳答案

终于找到了一个适用于不同 Android 版本/ROM/样式的解决方案。

主要问题是 EditText 的背景可绘制本身具有透明填充: enter image description here

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

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

为了实现这一点,我扫描了我从 EditText 的背景可绘制对象创建的位图。我自上而下和自下而上扫描它以找到完全透明线的数量,并将这些值用作我的 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;
}

它就像一个魅力!

it works!

关于android - 水平对齐 ImageView 和 EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9372159/

相关文章:

java - System.out.println 表示 out 无法解析或不是字段

Android - 用手指拖动 ImageView 不会更新位置

java - 分享 Intent 需要很长时间才能出现

android - 如何在水平 ScrollView 内的线性布局内制作 2 个 ImageView ,每个 ImageView 的大小都可以覆盖屏幕

java - 以编程方式添加到 TableRow 时,ImageView 不显示

android - 在长按时启用 EditText(或按钮)

java - 如何使用按钮将字符串值从一个 Activity 传递到两个不同的 Activity ?

android - 具有工作选择、光标位置等的自定义旋转 EditText View

Android sqlite3_limit?

php - BasicNetwork.performRequest : Unexpected response code 500, 安卓注册系统