【问题标题】:android how to align radio-buttons inside radio group?android如何对齐单选按钮组内的单选按钮?
【发布时间】:2017-01-16 02:48:10
【问题描述】:

我的活动中有一个类似表格的东西(我使用垂直 + 水平线性布局来制作这个表格),在这个表格中我有按钮、编辑文本和单选按钮。我需要将 radiogroup 内的单选按钮与所有表格相对应地对齐。但目前单选按钮看起来像是向左移动了。

我玩了每个单选按钮的重力,用无线电组的重力,它没有帮助。

这是最后 2 个水平线性布局的代码。我需要对齐单选按钮以像editText一样居中。

            <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:weightSum="6">

            <ImageView
                android:id="@+id/imageQuest"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <EditText
                android:id="@+id/quest1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:digits="0123456789-"
                android:ems="10"
                android:inputType="number"
                android:maxLength="3"
                android:hint="0"
                android:gravity="center" />

            <EditText
                android:id="@+id/quest2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:digits="0123456789-"
                android:ems="10"
                android:inputType="number"
                android:maxLength="3"
                android:hint="0"
                android:gravity="center" />

            <EditText
                android:id="@+id/quest3"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:digits="0123456789-"
                android:ems="10"
                android:inputType="number"
                android:maxLength="3"
                android:hint="0"
                android:gravity="center" />

            <EditText
                android:id="@+id/quest4"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:digits="0123456789-"
                android:ems="10"
                android:inputType="number"
                android:maxLength="3"
                android:hint="0"
                android:gravity="center" />

            <EditText
                android:id="@+id/quest5"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:digits="0123456789-"
                android:ems="10"
                android:inputType="number"
                android:maxLength="3"
                android:hint="0"
                android:gravity="center" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:weightSum="6">

            <ImageView
                android:id="@+id/imageLongestWay"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <RadioGroup
                android:id="@+id/RadioGroup"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="5"
                android:orientation="horizontal"
                android:weightSum="5">

                <RadioButton
                    android:id="@+id/radioButton1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1" />

                <RadioButton
                    android:id="@+id/radioButton2"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1" />

                <RadioButton
                    android:id="@+id/radioButton3"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1" />

                <RadioButton
                    android:id="@+id/radioButton4"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1" />

                <RadioButton
                    android:id="@+id/radioButton5"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1" />
            </RadioGroup>


        </LinearLayout>

截图如下: Radio-buttons are not aligned inside radio-group

【问题讨论】:

  • 将 android:gravity="center" 应用于包含单选按钮的线性布局
  • RadioGroup 继承自 LinearLayout。因此,您可以像在 LinearLayout 中一样使用 weights
  • android:gravity="center" 到包含单选按钮的线性布局 -> 没有帮助
  • RadioGroup 继承自 LinearLayout。因此,您可以像在 LinearLayout 中一样使用权重。 – Rotwang 昨天 -> 也没有帮助

标签: android xml radio-button radio-group


【解决方案1】:

您需要在drawable中创建自定义RadioButton

<RadioButton
      android:id="@+id/radioButton1"
      android:layout_width="0dp"
      android:layout_height="50dip"
      android:layout_weight="1"
      android:layout_gravity="center_horizontal"
      android:button="@android:color/transparent"
      android:drawableLeft="@drawable/custom_radio_button"/>

custom_radio_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_checked_radio_button" android:state_checked="true" android:state_pressed="false" />
    <item android:drawable="@drawable/ic_uncheked_radio_button" android:state_checked="false" android:state_pressed="false" />
</selector>

【讨论】:

  • 这是很好的尝试,自定义单选按钮看起来更大,但无论如何它并没有按照我的意愿对齐,它仍然移动到左侧
  • 您需要在drawable中添加图片,使用image Asset in将为hdpi、mdpi、xhdpi、xxhdpi创建图片。
  • 不,这里的图片资源是不必要的。您对自定义单选按钮的第一个建议非常好。几乎是对的。只需要将 "android:drawableLeft=" 替换为 "android:drawableTop=" 就可以很好地对齐了。
  • 单选按钮的图像资产解决方案看起来更大,而不是对齐。
【解决方案2】:

感谢S.B.K 的建议,我们需要使用自定义单选按钮图像。并使用“android:drawableTop="

所以 1)从互联网下载2张图片。一种用于检查的收音机,一种用于未检查的收音机。 2)把它们放到你项目中的drawable文件夹中 3)在drawable文件夹中创建custom_radio_button.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_checked_radio_button" android:state_checked="true" android:state_pressed="false" />
<item android:drawable="@drawable/ic_uncheked_radio_button" android:state_checked="false" android:state_pressed="false" />

4) 将单选按钮参数更改为

android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@android:color/transparent"
android:drawableTop="@drawable/custom_radio_button"/>

【讨论】:

    猜你喜欢
    • 2015-12-27
    • 1970-01-01
    • 2011-05-12
    • 1970-01-01
    • 2017-01-10
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多