如何根据屏幕尺寸设置单选按钮的宽度? (机器人)

时间:2010-11-23 18:09:52

标签: android size radio-button screen resolution

我有这些单选按钮,他们需要android:width="X"android:height"X"但是,我不知道如何设置这些属性,以便它们适应不同的屏幕尺寸。

以下是我用于按钮的代码:

    <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>

我已尝试将单选按钮放在表格行中,但之后它们无法正常运行。当我点击一个时,它选择了它,但是当我点击另一个时没有取消选择它。

我尝试将android:width替换为android:layout_width,但之后却没有显示。

我也尝试过使用dip,但是按钮没有显示出来。

我还应该补充一点,我使用的是drawables而不是stock单选按钮。我不知道这是否有所作为,但是drawables的大小都相同(50px x 50px)。

有谁知道我如何设置它们的高度和宽度,以便它们自己调整到不同的屏幕尺寸?

2 个答案:

答案 0 :(得分:8)

使用dp而不是px设置高度和宽度。有关这些单元的更多信息,请参阅此帖子的答案。 What is the difference between "px", "dp", "dip" and "sp" on Android?

我还建议你在支持多个屏幕时熟悉android的documentation

好的,所以我花了一些时间来制作一个针对Android 1.5的快速示例。

您可以找到source here

简而言之,您需要执行以下步骤:

将图片放入res/drawable。你应该至少有4个图标:Pressed&amp;未经检查,按下&amp;检查,未按下&amp;未经检查,未按下&amp;经过

在res / drawable中创建selector类型布局。这是我的:

<?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>

然后设置RadioGroup,如此:

<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>

我指定了50dp的尺寸,因为我的drawable的尺寸是50px x 50px。另请注意,我正在设置android:button而不是android:background

以上项目的签名APK:Custom RadioButton(注意:它针对SDK版本4,因此不会请求SD和手机权限)

这应该给出以下结果: Image of Custom RadioButton

希望这有帮助。

答案 1 :(得分:8)

我建议你尝试使用你的radioGroup:

            <RadioButton android:id="@+id/first"
                android:background="@drawable/button_radio" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:weight=1 />

            <RadioButton android:id="@+id/second"
                android:background="@drawable/button_radio"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:weight=1 />

            <RadioButton android:id="@+id/third"
                android:background="@drawable/button_radio" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:weight=1 />

            <RadioButton android:id="@+id/fourth"
                android:background="@drawable/button_radio"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:weight=1 />

            <RadioButton android:id="@+id/fifth"
                android:background="@drawable/button_radio"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:weight=1 />

</RadioGroup>

这样,所有单选按钮都占用相同的空间。而且,它会根据屏幕尺寸而改变。

相关问题