How to customize Android components according to image size using XML

I created a custom icon bar in Android using an XML layout creator using the RadioGroup in LinearLayout (see XML below). Each RadioButton in the RadioGroup has a custom (with checked and uncontrolled states) custom option with the android:button attribute.

The linear layout itself is located inside the RelativeLayout so that it can be displayed in an arbitrary position on the screen (in this case, it is a sidebar).

The output objects are 55 pixels wide, and the android:layout\_width all these views is wrap\_content .

When I make this component visible, aligned in the lower left corner of the screen, only about three quarters of the width of the RadioButton image are visible. Setting layout\_width from RelativeLayout to fill\_parent fixes this and causes a full button to appear.

However, this means that the button group uses click events across the entire width of the screen.

How can I make the whole button pressed, and only on the click response button? Hard coding of the width of the extruded is not a particularly desirable solution.

 <RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/LinearLayout02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_below="@+id/LinearLayout01" android:visibility="invisible"> <RadioGroup android:id="@+id/RadioGroup01" android:layout_height="wrap_content" android:checkedButton="@+id/RB01" android:layout_width="fill_parent"> <RadioButton android:id="@+id/RB01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:button="@drawable/DR01"/> <RadioButton android:id="@+id/RB02" android:layout_width="fill_parent" android:layout_height="wrap_content" android:button="@drawable/DR02"/> <RadioButton android:id="@+id/RB03" android:layout_width="fill_parent" android:layout_height="wrap_content" android:button="@drawable/DR03"/> <RadioButton android:id="@+id/RB04" android:layout_width="fill_parent" android:layout_height="wrap_content" android:button="@drawable/DR04"/> </RadioGroup> </LinearLayout> </RelativeLayout> 
+4
source share
2 answers

Setting the width of LinearLayout, RadioGroup or RadioButton in dp format (density other than density) may work for you. Based on what you said above, I think it should be the breadth of the layout. The sizes are hardcoded, but they will scale with the screen size.

So xml might look like this:

 android:layout_width="55dp" 

The official dps documentation is here

+7
source

I can not imagine why the other answer has 6 upvotes, this is completely wrong

The sizes are hard-coded, but they scale with the size of the screen.

No , they will scale based on screen density. I really want us to be able to use percentages from XML, instead of encoding them.

+1
source

All Articles