gon*_*lva 4 android alignment imageview android-edittext
我正在努力寻找一种在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>
Run Code Online (Sandbox Code Playgroud)
我也尝试了下面的许多建议,包括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>
Run Code Online (Sandbox Code Playgroud)
但结果完全一样.
我试过玩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;
}
Run Code Online (Sandbox Code Playgroud)
它就像一个魅力!
归档时间: |
|
查看次数: |
7342 次 |
最近记录: |