How to set the width of radio buttons according to the screen size? (robot)

I have these radio buttons. They need Android: width = "X" and Android: height "X". However, I don't know how to set these properties so that they can adapt to different screen sizes

This is my code for buttons:

    <RadioGroup android:layout_width="fill_parent"
        android:layout_height="50px" android:orientation="horizontal"
        android:checkedButton="@+id/first" android:id="@+id/states">

                <RadioButton android:id="@+id/first"
                    android:background="@drawable/button_radio" android:width="50px"
                    android:height="50px" />

                <RadioButton android:id="@+id/second"
                    android:background="@drawable/button_radio" android:width="50px"
                    android:height="50px" />

                <RadioButton android:id="@+id/third"
                    android:background="@drawable/button_radio" android:width="50px"
                    android:height="50px" />

                <RadioButton android:id="@+id/fourth"
                    android:background="@drawable/button_radio" android:width="50px"
                    android:height="50px" />

                <RadioButton android:id="@+id/fifth"
                    android:background="@drawable/button_radio" android:width="50px"
                    android:height="50px" />

    </RadioGroup>

I've tried to put radio buttons in table rows, but then they don't work properly. When I click one, it selects it, but when I click another, it doesn't deselect it

I've tried Android: layout_ Width replaces Android: width, but they are not displayed

I also tried to use dip, but the button didn't show up

I should also add that I use drawables instead of the stock radio button. I don't know if this will make a difference, but drawables are the same size (50px x 50px)

Who knows how I set the height and width of these so that they can adjust themselves to different screen sizes?

resolvent:

Use DP instead of PX to set the height and width. For more information about these units, see the answer in this post. What is the difference between "PX", "DP", "dip" and "SP" on Android?

I also recommend that you be familiar with Android documentation. Com when supporting multiple screens

OK, so I spent some time making a quick example for Android 1.5

You can find source here

In short, you need to perform the following steps:

Invalid Sign

Create selector type layout in RES / drawable. This is my:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_checked="true" android:state_pressed="false"
     android:drawable="@drawable/radio_on"/>
  <item android:state_checked="false" android:state_pressed="false"
     android:drawable="@drawable/radio_off"/>
  <item android:state_checked="true" android:state_pressed="true"
     android:drawable="@drawable/radio_on_pressed"/>
  <item android:state_checked="false" android:state_pressed="true"
     android:drawable="@drawable/radio_off_pressed"/>
</selector>

Then set up your radiogroup like this:

<RadioGroup android:layout_width="fill_parent"
   android:layout_height="50dp"
   android:orientation="horizontal"
   android:checkedButton="@+id/first">
   <RadioButton android:id="@+id/first"
      android:width="50dp"
      android:height="50dp"
      android:button="@drawable/button_radio"/>
   <RadioButton android:id="@+id/second"
      android:width="50dp"
      android:height="50dp"
      android:button="@drawable/button_radio"/>
   <RadioButton android:id="@+id/third"
      android:width="50dp"
      android:height="50dp"
      android:button="@drawable/button_radio"/>
   <RadioButton android:id="@+id/fourth"
      android:width="50dp"
      android:height="50dp"
      android:button="@drawable/button_radio"/>
</RadioGroup>

I specified the size of 50dp because the size of my drawable is 50px x 50px. Please also note that I set the Android: button instead of Android: background

This is the signature APK of the above project: Custom RadioButton (Note: its target is SDK version 4, so it will not request SD and phone permissions)

This should give the following results:

I hope this will help

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>