将UI项水平居中对齐

时间:2020-02-13 09:43:28

标签: android android-layout android-gridlayout

我正在尝试创建我的第一个Android应用,我想水平对齐屏幕上和行中的项目。

我尝试将layout_gravity设置为center_horizo​​ntal,如下所示,但是这似乎没有任何作用。我想念什么?

代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.wear.widget.BoxInsetLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/box_inset_layout_padding"
    tools:deviceIds="wear">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="@dimen/inner_frame_layout_padding"
        app:boxedEdges="all"
        android:minWidth="25px"
        android:minHeight="25px">
        <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"    
                android:rowCount="8"
                android:layout_gravity="center_horizontal"
                android:columnCount="1"
                android:orientation="horizontal">
            <TextView
                android:id="@+id/text"
                android:visibility="visible"
                android:textSize="9dp"
                android:layout_row="1"/>
            <TextView
                android:id="@+id/text2"
                android:visibility="visible"
                android:text="@string/hello_world"
                android:textAlignment="center"
                android:textSize="9dp"
                android:layout_row="2"/>
            <TextView
                android:id="@+id/serviceScore"
                android:layout_row="4"
                android:visibility="visible"
                android:textAlignment="center"
                android:textSize="50dp"/>
        </GridLayout>
    </FrameLayout>
</android.support.wear.widget.BoxInsetLayout>

UI:

enter image description here

1 个答案:

答案 0 :(得分:1)

尼斯的问候!只需添加布局的重力 center 而不是GridLayout的 center_horizo​​ntal

android:layout_gravity="center"