如何设置单选按钮的宽度以根据屏幕尺寸进行更改?(安卓)

How can I set the width of radio buttons to change with regards to screen size? (android)(如何设置单选按钮的宽度以根据屏幕尺寸进行更改?(安卓))

本文介绍了如何设置单选按钮的宽度以根据屏幕尺寸进行更改?(安卓)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have these radio buttons and they need android:width="X" and android:height"X" however, I do not know how to set these properties so that they adapt to a different screen size.

这是我用于按钮的代码:

Here is the code I'm using for my 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 have tried putting the Radio buttons in a table row, but then they don't operate properly. When I click one, it selected it, but didn't unselect it when I clicked another.

我尝试将 android:width 替换为 android:layout_width 但它们没有显示.

I have tried replacing the android:width with android:layout_width but then they don't show.

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

I have also tried using dip, but then the buttons didn't show up.

我还应该补充一点,我使用的是可绘制对象而不是普通单选按钮.我不知道这是否会有所不同,但可绘制对象的大小都相同(50px x 50px).

I should also add that I am using drawables instead of the stock radio buttons. I don't know if this would make a difference, but the drawables are all the same size (50px x 50px).

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

Does anyone know how I can set the height and width of these so they will adjust themselves to different screen sizes?

推荐答案

使用 dp 而不是 px 来设置高度和宽度.有关单位的更多信息,请参阅此帖子的答案.px"、dp"有什么区别, 浸"和sp"在安卓上?

Set the height and width using dp instead of px. See the answer to this post for more information on the units. What is the difference between "px", "dp", "dip" and "sp" on Android?

我还建议您熟悉 android 的关于支持多个屏幕.

I would also suggest you familiarize yourself with android's documentation on supporting multiple screens.

好的,所以我花了一点时间来创建一个针对 Android 1.5 的快速示例.

Okay, so I've taken a moment to whip up a quick example targeted at Android 1.5.

您可以在此处找到源代码.

简而言之,您需要采取以下步骤:

In short, you need to take the following steps:

将您的图像放在 res/drawable 中.您应该至少有 4 个图标: Pressed &未选中,按下 &已检查,未按下 &未选中,未按下 &已检查

Place your images in res/drawable. You should have at least 4 icons: Pressed & Unchecked, Pressed & Checked, Not Pressed & Unchecked, Not Pressed & Checked

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

Create a selector type layout in res/drawable. Here is mine:

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

Then set-up your RadioGroup like so:

<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 的尺寸,因为我的可绘制对象的尺寸是 50px x 50px.还要注意我设置的是 android:button 而不是 android:background.

I have specified dimension of 50dp as the dimension of my drawables are 50px x 50px. Also notice I am setting android:button and not android:background.

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

Here is a signed APK of the project above: Custom RadioButton (Note: it is targeted to SDK version 4 so it wouldn't request SD and Phone permissions)

这应该给出以下结果:

希望这会有所帮助.

这篇关于如何设置单选按钮的宽度以根据屏幕尺寸进行更改?(安卓)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何设置单选按钮的宽度以根据屏幕尺寸进行更改?(安卓)

基础教程推荐