Android Checkbox - Background + Button Layout - wrong size
I think I don't see an error in one of my layouts. I want to show a checkbox and a border around it.
I have generated images using Android Holo Colors Generator
I tried to add a shape to the checkbox, which worked fine on Nexus 4, but didn't display the button at all on any other device, so I added a dummy layout:
Markup:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5sp"
android:layout_marginTop="3sp"
android:background="@drawable/shape_button" >
<CheckBox
android:id="@+id/check"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:button="@drawable/btn_radio_holo_dark_hm" />
</LinearLayout>
the form:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/transparent"/>
<stroke
android:width="1px"
android:color="@color/gray" />
<corners
android:bottomLeftRadius="5sp"
android:bottomRightRadius="5sp"
android:topLeftRadius="5sp"
android:topRightRadius="5sp" />
</shape>
I now get the following:
I tried setting minWidth depending on mdpi / hdpi / xhdpi, but the button never appears centered on every device. fill_parent / wrap_content doesn't make any changes, also moving the button inside the layout.
Any suggestions on what is wrong here?
+3
source to share
1 answer
change outer layout to RelativeLayout and check centerInParent = true for checkbox
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5sp"
android:layout_marginTop="3sp"
android:background="@drawable/shape_button"
android:padding="2sp">
<CheckBox
android:id="@+id/check"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:button="@drawable/btn_radio_holo_dark_hm"
android:layout_centerInParent="true/>
</RelativeLayout>
+4
source to share