如何均匀分隔Android中的单选按钮?

时间:2013-03-15 22:58:09

标签: android radio-button radio-group cellspacing

我有三个radiobuttons,我想在屏幕上均匀分布它们。当我使用android:layout_weight="1"时,按钮会在屏幕上伸展。那么我如何在不同屏幕尺寸之间的每个屏幕之间有相同的空间?

<RadioGroup 
        android:id="@+id/auton_bar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingLeft="10dp"
        android:layout_below="@id/clear_fields"
                >
        <RadioButton
            android:id="@+id/auton_radio_1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" 
            android:background="@drawable/auton_col"
            android:layout_weight="1"

            />
        <!-- android:layout_marginRight="380dp"  --> 
        <RadioButton
            android:id="@+id/auton_radio_2"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" 
            android:background="@drawable/auton_col"
            android:layout_weight="1"


            />
        <RadioButton
            android:id="@+id/auton_radio_3"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" 
            android:background="@drawable/auton_col"
            android:layout_weight="1"
            />

    </RadioGroup>

2 个答案:

答案 0 :(得分:39)

如果您希望它们平均分享屏幕宽度,则需要在每个android:layout_width="match_parent"上设置View。你的xml会变成:

<RadioGroup
    android:id="@+id/auton_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/clear_fields"
    android:orientation="horizontal"
    android:paddingLeft="10dp" >

    <RadioButton
        android:id="@+id/auton_radio_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/auton_col" />
    <!-- android:layout_marginRight="380dp" -->

    <RadioButton
        android:id="@+id/auton_radio_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/auton_col" />

    <RadioButton
        android:id="@+id/auton_radio_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/auton_col" />
</RadioGroup>

详细说明,layout_weight可以两种方式使用。

  • 如果您在垂直线性布局中有多个视图,并且希望最后一个视图占用所有剩余空间,则可以将其高度设置为wrap_content,并为最后一个视图指定权重为1。
  • 如果您希望所有视图共享可用空间,请将所有宽度/高度设置为0dpmatch_parent,并为每个视图指定相同的权重值。他们将平等分享空间。

要获得背景可绘制比例,请创建一个位于drawable/文件夹中的新xml

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="center"
    android:src="@drawable/auton_col" />

将其命名为您喜欢的名称(例如auton_col_scale.xml),并将其作为您的背景引用。

答案 1 :(得分:3)

我注意到使用RadioButton的布局权重导致它们偏离中心对齐,尽管它们肯定每个共享50%的屏幕(它们是左对齐的)。为每个RadioButton设置重力仅导致文本居中/ facepalm

下面的XML显示了如何水平对齐两个单选按钮(可以是任何视图)并将它们居中:

const AWS = require("aws-sdk");

exports.handler = function( event, context, callback ) {
  const newImages = event.Records.map(
        (record) => AWS.DynamoDB.Converter.unmarshall(record.dynamodb.NewImage)
  );
  console.log('Converted records', newImages);
  callback(null, `Success`);
}
相关问题