UI布局

在安卓开发中,UI布局是通过不同的布局管理器(Layout)来控制界面元素的位置和排列。常用的布局类型有以下几种:

1. LinearLayout

  • 功能:将子视图按照水平方向(horizontal)或垂直方向(vertical)排列。

  • 常用属性

    • android:orientation:指定排列方向,horizontalvertical

    • android:layout_widthandroid:layout_height:定义布局的宽高。

    • android:weight:设置子视图占据的比例,结合 android:layout_widthandroid:layout_height 使用。

示例

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello, World!" />
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me" />
</LinearLayout>

2. RelativeLayout

  • 功能:根据父布局或其他子视图的相对位置来放置子视图,允许更多的灵活布局。

  • 常用属性

    • android:layout_alignParentTopandroid:layout_alignParentLeft 等:指定视图与父视图的对齐方式。

    • android:layout_toRightOfandroid:layout_below 等:指定视图相对于其他子视图的位置。

示例

3. ConstraintLayout

  • 功能:一种更灵活和强大的布局方式,可以通过创建不同的约束(Constraint)来控制视图之间的关系。

  • 常用属性

    • app:layout_constraintLeft_toLeftOfapp:layout_constraintTop_toTopOf:设置视图的约束。

    • app:layout_constraintWidth_percentapp:layout_constraintHeight_percent:按比例调整视图大小。

示例

4. FrameLayout

  • 功能:将所有子视图放置在屏幕的左上角(默认),并按顺序叠加。适用于堆叠布局,例如图片与文字叠加。

  • 常用属性

    • android:layout_gravity:控制视图在布局中的对齐方式。

示例

5. GridLayout

  • 功能:按网格排列子视图,可以指定每个视图在网格中的位置。

  • 常用属性

    • android:rowCountandroid:columnCount:设置行数和列数。

    • android:layout_rowandroid:layout_column:指定视图的行和列位置。

示例

6. TableLayout

  • 功能:将子视图按表格的形式排列,适合用于表格数据布局。

  • 常用属性

    • android:stretchColumns:定义哪些列可以被拉伸。

示例

Last updated