在android中对齐单选按钮 我正试图重新安排单选按钮

在android中对齐单选按钮 我正试图重新安排单选按钮,android,android-xml,Android,Android Xml,我有一个输出 搜索页面.xml <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="10dp" android:background="#E1E1E1" android:wei

我有一个输出

搜索页面.xml

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp"
        android:background="#E1E1E1"
        android:weightSum="1" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:text="City" />

        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:background="@drawable/rounded_edittext"
            android:layout_weight=".75" />
    </LinearLayout>

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="@android:color/black" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="selectDate"
        android:orientation="horizontal"
        android:padding="10dp"
        android:background="#E1E1E1"
        android:weightSum="1" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:text="date" />

        <EditText
            android:id="@+id/DATE_EDIT_TEXT_ID"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight=".75"
            android:background="@drawable/rounded_edittext"
            android:onClick="selectDate" />
    </LinearLayout>

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="@android:color/black" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp"
        android:background="#E1E1E1"
        android:weightSum="1" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:text="type" />

        <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/radio0"
            android:background="@drawable/yourbuttonbackground"
            android:button="@android:color/transparent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="Breakfast" />

        <RadioButton
            android:id="@+id/radio1"
            android:background="@drawable/yourbuttonbackground"
            android:button="@android:color/transparent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Lunch" />
    </RadioGroup>

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/yourbuttonbackground"
            android:button="@android:color/transparent"
            android:text="Dinner" />

    </LinearLayout>

按如下顺序间隔单选按钮


任何想法

将下面的代码复制到xml中。我已经更改了布局中的权重分布

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#E1E1E1"
        android:orientation="horizontal"
        android:padding="10dp"
        android:weightSum="2.5" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="type" />

        <RadioGroup
            android:id="@+id/radioGroup1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/radio0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="2dp"
                android:layout_weight=".5"
                android:background="@drawable/yourbuttonbackground"
                android:button="@android:color/transparent"
                android:checked="true"
                android:padding="5dp"
                android:text="Breakfast" />

            <RadioButton
                android:id="@+id/radio1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="2dp"
                android:layout_weight=".5"
                android:background="@drawable/yourbuttonbackground"
                android:button="@android:color/transparent"
                android:padding="5dp"
                android:text="Lunch" />
        </RadioGroup>

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="2dp"
            android:layout_weight=".5"
            android:background="@drawable/yourbuttonbackground"
            android:button="@android:color/transparent"
            android:padding="5dp"
            android:text="Dinner" />
    </LinearLayout>


希望这能起作用

我建议不要将radioGroup用于水平方向。我发现在执行此操作时,不同设备上都存在问题(Galaxy Note 2是最近出现的此类设备之一)

只需使用一个水平的线性布局,用边距将其向左推,使其对齐,并在其内部放置按钮,这些按钮将包含文本和背景形状

然后在代码中向它们添加click侦听器,并有一种保存上次单击哪个按钮的方法

您可以让state drawable包含selected=“true”状态和call按钮。setSelected(true或false)使其显示为选中或取消选中