[英]Align ImageView with EditText horizontally
我正在努力尋找一種在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(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的background drawable本身有透明填充:
此外,我注意到這種透明填充在不同的Android版本/ ROM /樣式之間變化很大 (例如,庫存ICS根本沒有透明填充)。
在中途結論中,我的原始代碼正確地將我的ImageView與我的EditText對齊。 但是,我真正想要的是將ImageView與EditText背景的可見部分對齊。
為實現這一點,我掃描了一個我從EditText的背景drawable創建的Bitmap。 我從上到下和自下而上掃描它以找到完全透明線的數量,並將這些值用作我的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;
}
它就像一個魅力!
要擺脫EditText中的填充,只需使用自定義圖像作為背景...帶有陰影的矩形就足夠了......或者你喜歡的任何東西......並使用自定義圖像將背景指定給EditText
android:background="@drawable/myBackground"
你需要使用RelativeLayout
與android:align_bottom
和android:align_Top
屬性。 我不是在編寫代碼。
您是否嘗試過EditText
的setCompoundDrawables()
方法。
您可以嘗試使用Drawable上的setBounds()
方法或使用setCompoundDrawablesWithInstrinsicBounds()
方法設置內部邊界。
這適用於所有設備。
我只是通過簡單地將以下內容添加到EditText來簡單地將EditText向下移動2dp來使用快速解決方法:
android:layout_marginBottom="-2dp"
android:layout_marginTop="2dp"
使用android:layout_centerInParent = true
編輯文本並查看它的工作天氣?
刪除editText和ImageView的屬性android:gravity =“center”並將linearlayout的gravity屬性設置為center_vertically。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.