如何设置单选按钮的宽度来改变与问候的屏幕尺寸? (机器人) [英] How can I set the width of radio buttons to change with regards to screen size? (android)

查看:148
本文介绍了如何设置单选按钮的宽度来改变与问候的屏幕尺寸? (机器人)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这些单选按钮,他们需要机器人:宽=X安卓高度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.

下面是我用我的按钮code:

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的宽度:layout_width 但当时他们没有表现出

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.

我还要补充一点,我使用的可绘,而不是股票的单选按钮。我不知道这是否会产生变化,但是可绘都是相同尺寸(50像素x 50像素)。

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,而不是像素。一看便知这个职位在单位的详细信息。 <一href="http://stackoverflow.com/questions/2025282/difference-of-px-dp-dip-and-sp-in-android">Difference的像素,DP,畅游和SP在Android的..

Set the height and width using dp instead of px. See the answer to this post for more information on the units. Difference of px, dp, dip and sp in 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.

您可以在这里找到源。

You can find the source here.

总之,你需要采取以下步骤:

In short, you need to take the following steps:

将您的图像 RES /绘制。你应该至少有4个图标:pressed和放大器;未选中,pressed和放大器;查了一下,不pressed和放大器;未选中,而不是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 /绘制布局。这里是我的:

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中像这样:

<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作为我可绘制尺寸

我指定的尺寸是50像素x 50像素。还要注意我设置安卓按钮,而不是机器人:背景

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:自定义单选(注:它是针对到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)

这应该给了以下结果:

This should give the following result:

希望这有助于。

这篇关于如何设置单选按钮的宽度来改变与问候的屏幕尺寸? (机器人)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆