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

发布于 2024-10-04 04:40:38 字数 1641 浏览 5 评论 0原文

我有这些单选按钮,它们需要 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,但按钮没有显示。

我还应该补充一点,我使用的是可绘制对象而不是库存单选按钮。我不知道这是否会产生影响,但可绘制的尺寸都是相同的(50px x 50px)。

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

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.

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纸短情长 2024-10-11 04:40:38

使用 dp 而不是 px 设置高度和宽度。有关单位的更多信息,请参阅这篇文章的答案。 “px”、“dp”、“之间有什么区别Android 上的“dip”和“sp”?

我还建议您熟悉 android 的 有关支持多屏幕的文档

好的,我花了一些时间快速编写了一个针对 Android 1.5 的示例。

您可以此处找到源

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

将图像放入 res/drawable 中。您应该至少有 4 个图标:按下和按下。未选中、已按下和已选中选中,未按下&未选中、未按下 &选中

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

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

这应该给出以下结果:
自定义单选按钮的图像

希望这会有所帮助。

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?

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

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

You can find the source here.

In short, you need to take the following steps:

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

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>

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>

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.

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)

This should give the following result:
Image of Custom RadioButton

Hope this helps.

当爱已成负担 2024-10-11 04:40:38

我建议您使用 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>

这样,所有单选按钮将占据相同的空间。此外,它会根据屏幕尺寸而变化。

I would suggest you to try the following with your 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>

This way all of your radio buttons will occupy equal space. Moreover, it will change as per the screen size.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文