【问题标题】:Align radio buttons inside radio group对齐单选组内的单选按钮
【发布时间】:2015-06-18 23:22:27
【问题描述】:

我正在开发一个 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>

但它不起作用。

我该怎么做?

【问题讨论】:

标签: android android-layout


【解决方案1】:

试试这个可能会有所帮助: 将您的单选组对齐到中心

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

【讨论】:

  • android:gravity="center" 是关键
  • 如果我想要一个单选按钮在左侧,另一个在右侧,另一个在顶部...??
【解决方案2】:

您是否尝试在 RadioGroup 中嵌套 LinearLayout 或 RelativeLayout 并将 RadioButton 放入其中?我自己没试过,不知道行不行。

【讨论】:

  • 如果你这样做,RadioGroup 将不起作用,因为 RadioButtons 必须是 RadioGroup 的直接子级;
猜你喜欢
  • 2017-01-16
  • 2015-12-27
  • 1970-01-01
  • 1970-01-01
  • 2014-03-17
  • 2011-05-12
  • 2021-01-20
  • 2015-01-25
  • 2020-07-23
相关资源
最近更新 更多