【问题标题】:How do you horizontally center a table inside a RelativeLayout in Android?如何在 Android 的 RelativeLayout 中水平居中表格?
【发布时间】: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 下面一行。我正在尝试使页脚中的整个表格在中心水平对齐。现在表格“卡”在屏幕的左侧。

标签: android android-layout


【解决方案1】:

试试这个答案

无需向 xml 添加更多布局

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:stretchColumns="0,1,2,3,4,5,6,7" ><!-- I have made change here -->

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="7" >

        <TextView
            android:id="@+id/labelA"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center" <!--set Gravity -->
            android:text="Aaa"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/labelB"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Bbb"
              android:gravity="center"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/labelC"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Ccc"
              android:gravity="center"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/labelD"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Ddd"
              android:gravity="center"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/labelE"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Eee"
              android:gravity="center"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/labelF"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Fff"
              android:gravity="center"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/labelG"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Ggg"
              android:gravity="center"
            android:textSize="18sp" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="7" >

        <CheckBox
            android:id="@+id/checkBoxA"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
              android:gravity="center"
            android:checked="true" />

        <CheckBox
            android:id="@+id/checkBoxB"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
              android:gravity="center"
            android:checked="true" />

        <CheckBox
            android:id="@+id/checkBoxC"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
              android:gravity="center"
            android:checked="true" />

        <CheckBox
            android:id="@+id/checkBoxD"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
              android:gravity="center"
            android:checked="true" />

        <CheckBox
            android:id="@+id/checkBoxE"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
              android:gravity="center"
            android:checked="true" />

        <CheckBox
            android:id="@+id/checkBoxF"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
              android:gravity="center"
            android:checked="true" />

        <CheckBox
            android:id="@+id/checkBoxG"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
              android:gravity="center"
            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:layout_above="@id/footer"
    android:layout_below="@id/header"
    android:background="#005"
    android:paddingTop="@dimen/activity_vertical_margin" >
</ScrollView>
<!-- End scrollable content -->

【讨论】:

  • 由于某种原因,复选框没有水平居中。它们仍然位于屏幕的左侧。
  • 我修好了。对于每个复选框,我将 android:gravity="center" 更改为 android:layout_gravity="center_horizo​​ntal"
【解决方案2】:
// try this way hope this will help you...
<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"
            android:gravity="center">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center">
                <TextView
                    android:id="@+id/labelA"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:textSize="18sp"
                    android:text="Aaa" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center">
                <TextView
                    android:id="@+id/labelB"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:textSize="18sp"
                    android:text="Bbb" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center">
                <TextView
                    android:id="@+id/labelC"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:textSize="18sp"
                    android:text="Ccc" />
            </LinearLayout>


            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center">
                <TextView
                    android:id="@+id/labelD"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:textSize="18sp"
                    android:text="Ddd" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center">
                <TextView
                    android:id="@+id/labelE"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:textSize="18sp"
                    android:text="Eee" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center">
                <TextView
                    android:id="@+id/labelF"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:textSize="18sp"
                    android:text="Fff" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center">
                <TextView
                    android:id="@+id/labelG"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:textSize="18sp"
                    android:text="Ggg" />
            </LinearLayout>

        </TableRow>

        <TableRow
            android:weightSum="7"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">


            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_weight="1">
                <CheckBox
                    android:id="@+id/checkBoxA"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:checked="true" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_weight="1">
                <CheckBox
                    android:id="@+id/checkBoxB"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:checked="true" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_weight="1">
                <CheckBox
                    android:id="@+id/checkBoxC"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:checked="true" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_weight="1">
                <CheckBox
                    android:id="@+id/checkBoxD"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:checked="true" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_weight="1">
                <CheckBox
                    android:id="@+id/checkBoxE"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:checked="true" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_weight="1">
                <CheckBox
                    android:id="@+id/checkBoxF"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:checked="true" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_weight="1">
                <CheckBox
                    android:id="@+id/checkBoxG"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:checked="true" />

            </LinearLayout>
        </TableRow>

    </TableLayout>
    <!-- End footer -->

    <!-- Scrollable content in middle of screen -->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_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>

【讨论】:

  • 行得通!因此,基本上,您通过将每行的每个组件放在各自的 LinearLayout 中来使它们居中。
  • 是的,因为如果您对所有“CheckBox”赋予相同的权重并设置 Gravity="center" 但“CheckBox”在右侧有文本,因此我们必须设置“CheckBox”宽度“wrap_content”并给出Gravity="center their parent layout",权重相同。
  • 我注意到,当我向中心 ScrollView 添加一个长的垂直小部件列表时,它们会溢出并强制滚动条出现,页脚中的 TextViews ("Aaa"、"Bbb"、" Cdd”等)变为“A”、“B”、“C”等。由于某种原因,某些文本正在消失。 Rahul 的解决方案不会发生这种情况。
  • 我已经对你的情况进行了测试,但我没有发生这种情况,所以你能告诉我你尝试测试的是哪种设备吗?
  • 我测试它的物理设备是 HTC Evo 3D(4.3 英寸屏幕,540x960 像素,Android 4.0.3)。
猜你喜欢
  • 2016-07-18
  • 2014-12-18
  • 2016-11-04
  • 2022-01-19
  • 1970-01-01
  • 2015-09-28
  • 2011-04-19
  • 2015-03-29
  • 1970-01-01
相关资源
最近更新 更多