Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何设置单选按钮的宽度以根据屏幕大小进行更改?(安卓)_Android_Size_Radio Button_Screen_Resolution - Fatal编程技术网

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

Android 如何设置单选按钮的宽度以根据屏幕大小进行更改?(安卓),android,size,radio-button,screen,resolution,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

我有这些单选按钮,它们需要
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:layout\u width
替换
android:width
,但是它们没有显示出来

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

我还应该补充一点,我使用的是拖拽功能,而不是股票单选按钮。我不知道这是否会有什么不同,但这些可拉丝都是相同的尺寸(50px 50px)


有人知道我如何设置这些屏幕的高度和宽度,以便他们可以根据不同的屏幕大小进行调整吗?

使用dp而不是px设置高度和宽度。有关单位的更多信息,请参阅本帖的答案

我还建议您熟悉android支持多屏幕的功能

好的,我花了一点时间快速制作了一个针对安卓1.5的示例

你可以找到那个

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

将图像置于
res/drawable
中。您应该至少有4个图标:按下并未选中、按下并选中、未按下并未选中、未按下并选中

在res/drawable中创建一个
选择器
类型布局。这是我的:

<?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>
我已经指定了50dp的尺寸,因为我的抽屉的尺寸是50px 50px。还要注意,我正在设置
android:button
,而不是
android:background

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

这将产生以下结果:


希望这能有所帮助。

我建议您与您的放射组一起尝试以下方法:

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



这样,您的所有单选按钮将占据相等的空间。此外,它将根据屏幕大小进行更改。

对不起,我应该说我尝试了dip,但是按钮没有显示出来。我将把它添加到OP中。我还将添加一个事实,即我使用的是可绘制按钮而不是标准按钮。如果您的按钮是实际图像,则创建3个不同的版本,并将它们放置在
res/drawable ldpi
res/drawable mdpi
res/drawable hdpi
文件夹中。为什么你要指定一个像素大小呢?我为所有版本的android都做了这个,现在可绘制的东西只在“可绘制”文件夹中。我也在1.5模拟器上运行它。我只是指定像素大小,因为这是按钮实际显示的唯一方式。它们显示的是表格,但不要取消选择,其他任何方式它们都不会显示。只是一个很大的黑色空间,他们应该在那里。我不确定在这一点上的解决方案可能是什么。我现在不能测试这个。但是,按钮确实需要在放射组中才能正常工作。您可能需要使用StateListDrawable。下面是一个某人做类似但略有不同的事情的示例:
            <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>