将 ImageView 与 EditText 水平对齐

Align ImageView with EditText horizontally(将 ImageView 与 EditText 水平对齐)

本文介绍了将 ImageView 与 EditText 水平对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying hard to find a way of aligning an EditText and an ImageView properly on Android. I keep getting this result:

XML 部分非常简单:

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>

我也尝试了以下许多建议,包括 PravinCG 的(RelativeLayout with alignTop/alignBottom):

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>

但结果完全一样.

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

I've tried playing with the EditText's background padding, intrinsic height, but to no avail.

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

The EditText's background drawable is different among Android versions/ROMs, and I want to support them all.

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

How can I make this alignment pixel perfect on any android version (and style)?

推荐答案

终于找到了适合不同Android版本/ROM/样式的解决方案.

Finally found a suitable solution that scales across different Android versions/ROMs/styles.

主要问题是 EditText 的背景可绘制对象本身具有透明填充:

The main problem is that the EditText's background drawable itself has transparent padding:

另外,我注意到这种透明填充在不同的 Android 版本/ROM/样式之间变化很大(例如,股票 ICS 根本没有透明填充).

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

总而言之,我的原始代码正确地将我的 ImageView 与我的 EditText 对齐.但是,我真正想要的是将 ImageView 与 EditText 背景的 visible 部分对齐.

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.

为了实现这一点,我扫描了从 EditText 的背景可绘制对象创建的位图.我从上到下和自下而上扫描它以找到完全透明线的数量,并将这些值用作我的 ImageView 的顶部/底部填充.在我的 N1 上,所有这些都持续不到 5 毫秒.代码如下:

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:

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

它就像一个魅力!

这篇关于将 ImageView 与 EditText 水平对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将 ImageView 与 EditText 水平对齐

基础教程推荐