android - 将单选按钮排列在文本正下方,并完美地位于所有 Android 设备的中心

标签 android radio-button radio-group android-compatibility

我想在文本下方排列单选按钮,但我做不到。我使用过 android:layout_marginLeft="20dp"但它仅适用于特定设备而不适用于所有设备。

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/txt1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#ff0000"
        android:gravity="center"
        android:text="red" />

    <TextView
        android:id="@+id/txt2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#00ff00"
        android:gravity="center"
        android:text="green" />

    <TextView
        android:id="@+id/txt3"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#0000ff"
        android:gravity="center"
        android:text="blue" />
</LinearLayout>

<RadioGroup
    android:id="@+id/rg_rgrp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="3" >

    <RadioButton
        android:id="@+id/rbtn_red"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:gravity="center" />

    <RadioButton
        android:id="@+id/rbtn_green"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center" />

    <RadioButton
        android:id="@+id/rbtn_blue"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center" />
</RadioGroup>

我同时使用了 Gravity 和 layout_gravity,但它适用于文本而不适用于单选按钮。

I want screen like this compatible for many devices

最佳答案

您的 RadioGroup 应该如下所示。无需代码。

<RadioGroup
    android:id="@+id/rg_rgrp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1" />
    <RadioButton
        android:id="@+id/rbtn_red"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />
    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="2" />
    <RadioButton
        android:id="@+id/rbtn_green"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />
    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="2" />
    <RadioButton
        android:id="@+id/rbtn_blue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />
    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1" />
</RadioGroup>

关于android - 将单选按钮排列在文本正下方,并完美地位于所有 Android 设备的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19490795/

相关文章:

Android Studio 标记为错误的 Android SQLite 查询

javascript - 基于多个单选按钮切换显示/隐藏

jquery - 以编程方式选中取消选中是否单选按钮 jQuery Mobile

jquery - 如果使用 jquery 检查单选按钮,则启用输入字段

jquery - symfony 表单在输入类型 ="choice"上使用 jquery 更改方法

javascript - Angular JS 默认选中单选按钮

java - 能够选择同一组中的两个单选按钮

标题中的 Android,类似 iphone 的后退按钮作为布局的一部分

android - 在 android 上使用图像 Sprite

android - 从 Android 库项目访问资源?