首先搜索了一个解决方案(例如here),我找到了以下解决方案:
android:layout_centerHorizontal="true"
这对我不起作用(见第三个按钮)。下面是RelativeLayout
中三个按钮的完整示例代码,其中中间按钮应居中(水平和垂直,按钮为),另外两个按钮应该对称放置在屏幕上,水平居中。但是,他们不是,使用我多次发现的建议解决方案。
那我该错过什么?
完整代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/checkscreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.impyiablue.checkpoint.CheckScreen">
<RelativeLayout
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="@+id/check_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/check_next"
android:textSize="20sp"
android:padding="25dip"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/check_now"
android:layout_alignStart="@+id/check_now"
android:layout_marginTop="50dp" />
<Button
android:id="@+id/check_now"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/check_now"
android:padding="70dip"
android:textSize="20sp" />
<Button
android:id="@+id/check_redo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="@string/check_redo"
android:textSize="20sp"
android:padding="25dip"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/check_now"
android:layout_alignStart="@+id/check_now"
android:layout_marginBottom="50dp" />
</RelativeLayout>
</LinearLayout>
答案 0 :(得分:3)
`
<Button
android:id="@+id/check_redo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="@string/check_redo"
android:textSize="20sp"
android:padding="25dip"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp" />
` 您可以尝试使用RelativeLayout作为根布局,这将使事情更容易管理。
答案 1 :(得分:0)
我建议你使用weightSum和layout_weight属性水平创建3个按钮,只显示中间的按钮,通过在另外2个(第一个和最后一个)上应用INVISIBLE可见性
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
$scope.carList = [];
$scope.accesoriesList = [];
$http.get("carList.htm").then(function(data) {
for (var i = 0; i < data.length; i++) {
$scope.carList.push({"id": data[i].carId, "name": data[i].carName});
}
for (var i = 0; i < $scope.carList.length; i++) {
$http.get("accesoriesList.htm").then(function(accesories) {
$scope.accesoriesList.push({"id": carId, "name": carName, "accesories" : accesories});
});
}
});
});
请添加任何必要的参数,此代码只是为了演示这个想法。
答案 2 :(得分:0)
在android:layout_gravity=center
上使用button
。示例 -
<Button
android:id="@+id/check_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/check_next"
android:textSize="20sp"
android:padding="25dip"
android:layout_gravity=center
android:layout_marginTop="50dp" />
或者对父视图或布局的所有子视图进行居中对齐,只需在父视图或布局上使用android:gravity=center
答案 3 :(得分:0)
这样的布局应该使用LinearLayout进行,因为它支持体重,而RelativeLayout则不支持! Android Studio中完全正常工作和测试的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/checkscreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="layout_centerHorizontal"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/check_cancel"
android:layout_width="wrap_content"
android:layout_height="0sp"
android:layout_marginTop="50dp"
android:layout_weight="1"
android:padding="25dip"
android:text="check_next"
android:textSize="20sp" />
<Button
android:id="@+id/check_now"
android:layout_width="wrap_content"
android:layout_height="0sp"
android:layout_weight="1"
android:padding="70dip"
android:text="check_now"
android:textSize="20sp" />
<Button
android:id="@+id/check_redo"
android:layout_width="wrap_content"
android:layout_height="0sp"
android:layout_marginBottom="50dp"
android:layout_weight="1"
android:padding="25dip"
android:text="check_redo"
android:textSize="20sp" />
</LinearLayout>