【发布时间】:2014-04-17 13:01:53
【问题描述】:
我有一个带有页眉、中心可滚动内容和页脚的布局。
页脚是一个包含两行的 TableLayout。
问题是行总是与屏幕左侧对齐,但我想让整个表格水平居中。
我整天都在处理这个问题,但似乎无法弄清楚。我尝试使用各种重力属性将其居中,但表格始终向左对齐。
这是一张图片:
http://postimg.org/image/jr40dopzj/
您可以看到页脚中的 TableLayout 是如何水平左对齐的。
这是我现在拥有的代码:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Header -->
<RelativeLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#FC9" >
<!-- "Label1:" and editText1 -->
<TextView
android:id="@+id/label1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Label 1:"
android:textSize="20sp"
android:textStyle="bold" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/label1"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/label1"
android:ems="8"
android:inputType="textMultiLine"
android:textSize="20sp" >
</EditText>
<!-- "Label 2:" and editText2 -->
<TextView
android:id="@+id/label2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/label1"
android:layout_below="@+id/label1"
android:paddingTop="26dp"
android:text="Label 2:"
android:textSize="20sp"
android:textStyle="bold" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/label2"
android:layout_alignLeft="@+id/editText1"
android:layout_toRightOf="@+id/label2"
android:ems="8"
android:inputType="textMultiLine"
android:textSize="20sp" />
</RelativeLayout>
<!-- End header -->
<!-- Footer -->
<!-- I cannot center this TableLayout horizontally. -->
<!-- It is always aligned to the left. -->
<TableLayout
android:id="@+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#FFF" >
<TableRow
android:weightSum="7"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/labelA"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Aaa" />
<TextView
android:id="@+id/labelB"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Bbb" />
<TextView
android:id="@+id/labelC"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Ccc" />
<TextView
android:id="@+id/labelD"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Ddd" />
<TextView
android:id="@+id/labelE"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Eee" />
<TextView
android:id="@+id/labelF"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Fff" />
<TextView
android:id="@+id/labelG"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Ggg" />
</TableRow>
<TableRow
android:weightSum="7"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="@+id/checkBoxA"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checked="true" />
<CheckBox
android:id="@+id/checkBoxB"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checked="true" />
<CheckBox
android:id="@+id/checkBoxC"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checked="true" />
<CheckBox
android:id="@+id/checkBoxD"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checked="true" />
<CheckBox
android:id="@+id/checkBoxE"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checked="true" />
<CheckBox
android:id="@+id/checkBoxF"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checked="true" />
<CheckBox
android:id="@+id/checkBoxG"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checked="true" />
</TableRow>
</TableLayout>
<!-- End footer -->
<!-- Scrollable content in middle of screen -->
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="@dimen/activity_vertical_margin"
android:layout_below="@id/header"
android:layout_above="@id/footer"
android:background="#005" >
</ScrollView>
<!-- End scrollable content -->
</RelativeLayout>
【问题讨论】:
-
尝试将表格布局的方向更改为水平
-
它仍然向左对齐。
-
你希望所有的文本框都在垂直的直线上???并且居中对齐
-
您可以尝试将
android:layout_gravity="center_horizontal"添加到您的页脚TableLayout。 -
@KaranMer 不,我只关心页脚,它包含两行 - 一行 TextViews 和 CheckBoxes 下面一行。我正在尝试使页脚中的整个表格在中心水平对齐。现在表格“卡”在屏幕的左侧。