问题
I have a layout which contains a TextView and a CheckBox, and this looks as expected on my Galaxy Nexus (4.2.2):
However when viewing on a Galaxy S3 and S2, I get some odd padding to the top, right and bottom of the CheckBox which is messing up the horizontal alignment of the whole subview:
The XML layout:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_vert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<LinearLayout
android:id="@+id/ll_horz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="Some Text"
android:textSize="14sp" />
<CheckBox
android:id="@+id/cb"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
It's even more obvious if I used a selector for a custom checkbox resource (which I'm ultimately trying to do):
<CheckBox
android:id="@+id/cb"
android:gravity="center"
android:button="@drawable/selector_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Galaxy S3:
Galaxy Nexus:
Anyone else experienced this? Obviously the gravity="center" appears to have no effect on TouchWiz...
EDIT 10-SEP-2013 -
I hacked in a fix
if (android.os.Build.MANUFACTURER.equals("samsung") && android.os.Build.MODEL.startsWith("GT-")) {
LinearLayout llHorz = (LinearLayout)view.findViewById(R.id.ll_horz);
llHorz.setPadding(Math.round(getResources().getDisplayMetrics().density * 20f), llHorz.getPaddingTop(), llHorz.getPaddingRight(), llHorz.getPaddingBottom());
}
This is less than ideal but it serves its purpose for now.
回答1:
Instead of using a separate CheckBox and TextView, you can do this (the checkbox will appear on the left and the text will be padded 10dip to the right):
<CheckBox
android:id="@+id/checkBox1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:drawableLeft="@android:color/transparent"
android:drawablePadding="10dp"
android:text="Some Text"/>
The trick is to set colour to transparent for android:drawableLeft
and assign a value for android:drawablePadding
. Also, transparency allows you to use this technique on any background colour without the side effect - like colour mismatch.
Or
You can check this out.
回答2:
had the same issue with an galaxy s2. Tested at the Samsung Remote Test Lab with the Result, that also the Android version is relevant. Seems that Samsung has fixed it on newer Versions.
if (Build.MANUFACTURER.equals("samsung") && Build.MODEL.startsWith("GT-") && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1)
来源:https://stackoverflow.com/questions/18575923/android-checkbox-extra-padding-on-touchwiz