将单选按钮对齐在单选组内

14

我正在开发一款Android 3.1平板应用程序,我在这里询问是因为我在我的代码中没有找到任何有效的答案(或示例)。

我有以下代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1" >

    <LinearLayout
        android:id="@+id/materialsLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
                                                <!-- 0 . Título del fragment -->
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight=".5"
            android:gravity="center_horizontal"
            android:text="@string/layout_title_quantity"
            android:textAppearance="?android:attr/textAppearanceLarge" />
                                                <!-- 1. Texto -->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/layout_qty_size_carton"
            android:textAppearance="?android:attr/textAppearanceMedium" />
                                                <!-- 2. Texto -->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dip"
            android:layout_gravity="center"
            android:orientation="horizontal"
            android:weightSum=".45" >

            <TextView
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight=".1"
                android:gravity="right"
                android:text="@string/layout_req_po"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight=".05"
                android:text="@string/layout_qty_carton"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/qtyPerCartonTxt"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight=".05"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight=".01"
                android:gravity="center"
                android:text="@string/layout_slash"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight=".05"
                android:text="@string/layout_cartoon_size"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/cartonSizeTxt"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight=".05"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight=".01"
                android:gravity="center"
                android:text="@string/layout_slash"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight=".03"
                android:text="@string/layout_weight"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/cartonWeightTxt"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight=".05"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </LinearLayout>
                                                <!-- 3. Titulo tabla -->
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/layout_actual_finding"
                android:textAppearance="?android:attr/textAppearanceMedium" />
                                                <!-- 4. Cabecera tabla -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dip"
            android:layout_gravity="center"
            android:orientation="horizontal"
            android:weightSum=".5" >

            <TextView
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight=".07"
                android:gravity="center"
                android:text="@string/layout_cartoon_number"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight=".07"
                android:gravity="center"
                android:text="@string/layout_actual_qty"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight=".07"
                android:gravity="center"
                android:text="@string/layout_actual_size"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight=".07"
                android:gravity="center"
                android:text="@string/layout_actual_weight"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <Button
                android:id="@+id/btnAddNewActPckCtr"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight=".05"
                android:onClick="onAddNewActPckCtrClick"
                android:text="@string/btn_add_new" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dip"
            android:layout_gravity="center"
            android:orientation="horizontal"
            android:weightSum=".5" >

            <TextView
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight=".07"
                android:gravity="right"
                android:text="@string/layout_result"
                android:textAppearance="?android:attr/textAppearanceMedium" />


            <RadioGroup
                android:id="@+id/rGroupResQTYOnline"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_gravity="right"
                android:layout_weight=".07"
                android:orientation="horizontal" >


                <RadioButton
                    android:id="@+id/rResQTYOnlineYes"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="right"
                    android:text="@string/layout_yes" />

                <RadioButton
                    android:id="@+id/rResQTYOnlineNo"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="left"
                    android:text="@string/layout_no" />

            </RadioGroup>

            <RadioGroup
                android:id="@+id/rGroupResSzOnline"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight=".07"
                android:orientation="horizontal" >

                <RadioButton
                    android:id="@+id/rResSzOnlineYes"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="@string/layout_yes" />

                <RadioButton
                    android:id="@+id/rResSzOnlineNo"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="@string/layout_no" />
            </RadioGroup>

            <RadioGroup
                android:id="@+id/rGroupResWgOnline"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight=".07"
                android:orientation="horizontal" >

                <RadioButton
                    android:id="@+id/rResWgOnlineYes"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="@string/layout_yes" />

                <RadioButton
                    android:id="@+id/rResWgOnlineNo"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="@string/layout_no" />
            </RadioGroup>



        <Button
            android:id="@+id/btnTakeQtyOnlinePhoto"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".07"
            android:onClick="onTakePhotoClick"
            android:text="@string/btn_take_photo" />

        </LinearLayout>


        <TextView
            android:layout_width="match_parent"
            android:layout_height="40dip"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="40dip"
            android:layout_marginTop="40dip"
            android:layout_weight=".1"
            android:gravity="left|center_vertical"
            android:text="@string/layout_photos"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <Gallery
            android:id="@+id/quantityOnlineGallery"
            android:layout_width="fill_parent"
            android:layout_height="200dip" />
</LinearLayout>
</ScrollView>

我想将单选按钮居中放置在radiogroup内,但不知道该如何实现。

我尝试了以下代码:

 <RadioGroup
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
   <RadioButton
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       android:layout_gravity="center|left"/>
   <RadioButton
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_gravity="center|right" />
    </RadioGroup>

但是它不起作用。

我该怎么做?


请发布您的完整XML代码。 - Dipak Keshariya
问题已更新,包含完整的 XML 代码。 - VansFannel
你想要怎样的?你能展示一张图片吗? - Chintan Khetiya
请查看此答案:https://dev59.com/E2kv5IYBdhLWcg3wpCMz#17607367 - ODAXY
3个回答

22

你可以尝试以下方法: 将单选按钮组居中对齐

<RadioGroup
                android:id="@+id/rGroupResSzOnline"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight=".07"
                android:gravity="center"
                android:orientation="horizontal" >

                <RadioButton
                    android:id="@+id/rResSzOnlineYes"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="Yes" />

                <RadioButton
                    android:id="@+id/rResSzOnlineNo"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="No" />
            </RadioGroup>

1
android:gravity="center" 是关键。 - Mike-Bell
如果我想要一个单选按钮在左侧,另一个在右侧,还有一个在顶部...怎么办? - Devashish Prasad

0

由于RadioGroup覆盖了LinearLayout,您可以将RadioButton与LinearLayout的子视图类似地对齐。

  1. 如果您想将所有子RadioButton居中对齐:<RadioGroup.... android:gravity="center" ...>
  2. 如果您想要一个RadioButton在开头,另一个在结尾:
    <RadioButton1... android:layout_weight="1".../>
    <RadioButton2... android:layout_weight="1".../>
    它将为两个RadioButton分配相等的空间。如果您想要强制将RadioButton2对齐到最右边,只需不给它分配layout_weight即可。

-5

你尝试过在RadioGroup中嵌套一个LinearLayout或RelativeLayout,并将RadioButton放在其中吗?我自己没有尝试过,所以不知道它是否有效。


3
如果你这样做,RadioGroup就不能正常工作,因为RadioButton必须是RadioGroup的直接子元素。 - Serhii Nadolynskyi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接