安卓开发笔记 设置视图的对齐方式

java yekong 401℃

设置视图的对齐方式有两种途径:
采用layout_gravity属性,它指定了当前视图相对于上级视图的对齐方式。
采用gravity属性,它指定了下级视图相对于当前视图的对齐方式。
layout_gravity与gravity的取值包括:left、 top、 right、 bottom、center,还可以用竖线连接各取值,例如 “left|top〞 表示即靠左又靠上,也就是朝左上角对齐。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_gravity="center"
        android:background="#00ffff"
        android:gravity="center">

        <View
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:background="#ff0000"></View>
    </LinearLayout>
</LinearLayout>

安卓开发笔记 设置视图的对齐方式

喜欢 (0)