2010-11-23 96 views
3

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

下面是我用我的按钮代码:

<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_width代替android:width,但是他们没有显示。

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

我还应该补充说我使用的是drawables而不是stock单选按钮。我不知道这是否会有所作为,但可绘制的尺寸都是相同的(50px x 50px)。

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

回答

7

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

我还建议你在支持多屏的情况下熟悉Android的documentation

好的,所以我花了一点时间来掀起一个针对Android 1.5的快速示例。

你可以找到source here

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

将您的图像res/drawable。你应该有至少4个图标:按下&未选中,按下&经过,未按&未经检查,未按&经过

创建RES /绘制一个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的尺寸作为我可绘制的尺寸为50像素X 50像素。另请注意,我正在设置android:button而不是android:background

这里是上述项目的签署APK:Custom RadioButton(注:这是针对SDK版本4级所以它不会要求SD和电话的权限)

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

希望这会有所帮助。

+0

对不起,我应该说我尝试了浸,但后来没有显示按钮。我会将其添加到OP。我还会添加一个事实,即使用drawable而不是标准按钮。 – NotACleverMan 2010-11-23 18:20:30

+1

如果你的按钮是实际的图像,然后创建3个不同的版本,并将它们放在`res/drawable-ldpi`,`res/drawable-mdpi`,`res/drawable-hdpi`文件夹中。你为什么要指定像素大小呢? – Thomas 2010-11-23 18:22:08

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> 

这样,所有的单选按钮,将占据相等空间。此外,它会根据屏幕大小而改变。

相关问题