button needs to fill parent radiobutton

房东的猫 提交于 2021-02-08 15:28:12

问题


I have a radio group with two radiobuttons. each button has a custom selector. I would like for the drawable in the selector to fill out the width of the radio button. Under different resolutions the button looks different. I am not using a nine-patch so this makes sense but even if i was I am not sure how to make the button drawable stretch. Below is the code for the radio group and a selector. widths are different from fiddling around.

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

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

The problem is when testing under different resolutions gaps form between the buttons. Thanks for any help.


回答1:


RadioGroup is actually a subclass of LinearLayout so what you could do is use layout_weight of the children to control the spacing. This might be a little helpful: Linear Layout and weight in Android and this 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>

You are going to need to use ninepatch and maybe use padding left or right if there is text on the radio buttons.



来源:https://stackoverflow.com/questions/8071027/button-needs-to-fill-parent-radiobutton

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!