我有三个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>
答案 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。0dp
或match_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`);
}