【问题标题】:button needs to fill parent radiobutton按钮需要填充父单选按钮
【发布时间】:2011-12-25 15:24:06
【问题描述】:

我有一个带有两个单选按钮的单选组。每个按钮都有一个自定义选择器。我希望选择器中的可绘制对象填充单选按钮的宽度。在不同的分辨率下,按钮看起来不同。我没有使用九个补丁,所以这是有道理的,但即使我是,我也不确定如何使按钮可拉伸。下面是无线电组和选择器的代码。宽度与摆弄不同。

布局xml:

            <RadioButton
                android:id="@+id/radiomale"
                android:layout_width="126dp"
                android:layout_height="50dp"
                android:minHeight="50dp"
                android:minWidth="126dp"
                android:button="@layout/signupmale"

                />

            <RadioButton
                android:id="@+id/radiofemale"
                 android:layout_width="128dp"
                android:layout_height="50dp"
                android:minHeight="50dp"
                android:minWidth="128dp"
                android:button="@layout/signupfemale" />
        </RadioGroup>

选择器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_window_focused="false"
    android:drawable="@drawable/genre_male_pressed" />
<item android:state_checked="false" android:state_window_focused="false"
    android:drawable="@drawable/genre_male" />
<item android:state_checked="true" android:state_pressed="true"
    android:drawable="@drawable/genre_male_pressed" />
<item android:state_checked="false" android:state_pressed="true"
    android:drawable="@drawable/genre_male" />
<item android:state_checked="true" android:state_focused="true"
    android:drawable="@drawable/genre_male_pressed" />
<item android:state_checked="false" android:state_focused="true"
    android:drawable="@drawable/genre_male" />
<item android:state_checked="false" android:drawable="@drawable/genre_male" android:scaleType="fitXY" />
<item android:state_checked="true" android:drawable="@drawable/genre_male_pressed" android:scaleType="fitXY"/>
</selector>

问题是在不同分辨率下测试时,按钮之间会形成间隙。谢谢你的帮助。

【问题讨论】:

    标签: android layout


    【解决方案1】:

    RadioGroup 实际上是LinearLayout 的子类,因此您可以使用子类的 layout_weight 来控制间距。这可能有点帮助:Linear Layout and weight in Android 还有这个Android layout_weight comportment

        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="45dip"
            android:weightSum="1"
            android:orientation="horizontal"
            >
    
            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="40dip"
                android:layout_weight=".5"
                />
    
            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="40dip"
                android:layout_weight=".5"
                />
    
        </RadioGroup>
    

    如果单选按钮上有文本,您将需要使用 Ninepatch,并且可能使用向左或向右填充。

    【讨论】:

    • 感谢您的回复。我尝试为单选按钮和嵌套按钮添加布局权重和权重值。不行 :(。还设置了重量总和。
    • 确保设置 layout_width 或 height 取决于您希望按钮拉伸到 fill_parent 的方式。
    • 谢谢,我将在按钮上使用九个补丁。试图避免这样做,因为按钮的某些部分不易拉伸:(。再次感谢。
    • 如果您发布按钮,我可能会帮助您修补它以使其正常工作,也可能不是:x
    • 为我的 radioGroup 完美工作,谢谢
    猜你喜欢
    • 2013-05-01
    • 2021-12-24
    • 2020-10-27
    • 1970-01-01
    • 2021-02-02
    • 2012-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多