久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

將 ImageView 與 EditText 水平對(duì)齊

Align ImageView with EditText horizontally(將 ImageView 與 EditText 水平對(duì)齊)
本文介紹了將 ImageView 與 EditText 水平對(duì)齊的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

限時(shí)送ChatGPT賬號(hào)..

我正在努力尋找一種在 Android 上對(duì)齊 EditTextImageView 正確 的方法.我不斷得到這個(gè)結(jié)果:

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

XML 部分非常簡(jiǎn)單:

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>

但結(jié)果完全一樣.

我嘗試過(guò)使用 EditText 的背景填充、固有高度,但無(wú)濟(jì)于事.

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 版本(和樣式)上使這種對(duì)齊像素完美?

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.

主要問(wèn)題是 EditText 的背景可繪制對(duì)象本身具有透明填充:

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

另外,我注意到這種透明填充在不同的 Android 版本/ROM/樣式之間變化很大(例如,股票 ICS 根本沒(méi)有透明填充).

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 對(duì)齊.但是,我真正想要的是將 ImageView 與 EditText 背景的 visible 部分對(duì)齊.

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.

為了實(shí)現(xiàn)這一點(diǎn),我掃描了從 EditText 的背景可繪制對(duì)象創(chuàng)建的位圖.我從上到下和自下而上掃描它以找到完全透明線的數(shù)量,并將這些值用作我的 ImageView 的頂部/底部填充.在我的 N1 上,所有這些都持續(xù)不到 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;
}

它就像一個(gè)魅力!

這篇關(guān)于將 ImageView 與 EditText 水平對(duì)齊的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Cut, copy, paste in android(在android中剪切、復(fù)制、粘貼)
android EditText blends into background(android EditText 融入背景)
Change Line Color of EditText - Android(更改 EditText 的線條顏色 - Android)
EditText showing numbers with 2 decimals at all times(EditText 始終顯示帶 2 位小數(shù)的數(shù)字)
Changing where cursor starts in an expanded EditText(更改光標(biāo)在展開(kāi)的 EditText 中的開(kāi)始位置)
EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView問(wèn)題)
主站蜘蛛池模板: 日韩视频高清 | 成人在线中文字幕 | 91麻豆精品国产91久久久久久 | 欧美成人精品一区二区三区 | 96av麻豆蜜桃一区二区 | 日本精品裸体写真集在线观看 | 91在线观看免费 | 日韩精品久久久久久 | 91精品国产一区二区三区蜜臀 | 国产精品美女视频 | 久久99久久 | 中文字幕亚洲国产 | 国产第二页 | 日本在线中文 | 久久成人免费视频 | 成人在线视频一区二区三区 | 精品在线播放 | 日韩一区在线播放 | 成人超碰| 久久999| 国产精品久久久久久久久久 | 九九一级片 | 天堂国产 | 国产亚洲欧美在线 | 中文字幕日韩欧美一区二区三区 | 欧美亚洲一级 | 成人免费一区二区三区牛牛 | 国产不卡在线播放 | 中文字幕一区二区三区在线视频 | 成人免费毛片片v | 91久久久久久久久久久久久 | 97国产精品视频 | 中文字幕成人网 | 在线观看成人小视频 | 中文字幕精品一区二区三区精品 | 污视频在线免费观看 | 电影91久久久 | 91视频导航| 在线播放国产一区二区三区 | 观看av | 国产精品久久视频 |