Android - 布局方式学习

2022-06-15  本文已影响0人  Bruce_XHG

LinearLayout线性布局

1、线性布局方向有两个:horizontal、 vertical 默认horizontal
horizontal


image.png
horizontal
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/teal_200"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<Button
    android:text="LinearLayout布局1"
    android:textAllCaps="false"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

    <Button
        android:text="LinearLayout布局2"
        android:textAllCaps="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

vertical


image.png
vertical
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/teal_200"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<Button
    android:text="LinearLayout布局1"
    android:textAllCaps="false"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

    <Button
        android:text="LinearLayout布局2"
        android:textAllCaps="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

这里 layout_marginLeft相对于父视图而言,layout_marginTop 相对于上面的 button 而言(如果上面没有其他 view 那么就相对于父视图,如果有其他 view 那么就相对于离他最近的 view 来说)


image.png
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/teal_200"
    android:orientation="vertical">

    <Button
        android:id="@+id/button_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LinearLayout布局1"
        android:textAllCaps="false" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:text="LinearLayout布局2"
        android:textAllCaps="false" />
</LinearLayout>

LinearLayout 嵌套使用


image.png
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/teal_200"
    android:orientation="vertical">

    <Button
        android:id="@+id/button_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LinearLayout布局1"
        android:textAllCaps="false" />

    <Button
        android:id="@+id/button_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:text="LinearLayout布局2"
        android:textAllCaps="false" />

    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_marginTop="20dp"
        android:background="@color/red"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button_three"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="LinearLayout布局1"
            android:textAllCaps="false" />

        <Button
            android:id="@+id/button_four"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="20dp"
            android:text="LinearLayout布局2"
            android:textAllCaps="false" />
    </LinearLayout>

</LinearLayout>

gravity、layout_gravity的使用


image.png
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/teal_200"
    android:gravity="center_vertical"//设置全部子视图垂直方向居中
    android:orientation="vertical">

    <Button
        android:id="@+id/button_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LinearLayout布局1"
        android:layout_gravity="center"//设置居中显示,但是线性布局限制了可布局的高度就是自己的高度上可水平移动
        android:textAllCaps="false" />//设置 text 为字母时 默认是大写的 这里设置为非大写

    <Button
        android:id="@+id/button_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LinearLayout布局2"
        android:textAllCaps="false" />

    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_marginTop="20dp"
        android:background="@color/red"
        android:layout_gravity="center"//相对于外层来说,该 view 在自己的高度范围内水平居中
        android:orientation="horizontal">

        <Button
            android:id="@+id/button_three"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="LinearLayout布局1"
            android:textAllCaps="false" />

        <Button
            android:id="@+id/button_four"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="20dp"
            android:text="LinearLayout布局2"
            android:textAllCaps="false" />
    </LinearLayout>

</LinearLayout>

RelativeLayout相对布局

image.png

同 LinearLayout 一样 也可以设置 gravity 来设置子视图。但是子视图无法再设置 layout_gravity

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/teal_700">

    <Button
        android:id="@+id/relative_Btn_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="20dp"
        android:text="RelativeLayout布局 1"
        android:textAllCaps="false" />

    <Button
        android:id="@+id/relative_Btn_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/relative_Btn_1"
        android:layout_marginLeft="40dp"
        android:layout_toRightOf="@+id/relative_Btn_1"//这里通过 id 获取第一个 btn就是相对于第一个 button 设置布局
        android:text="RelativeLayout布局 2"
        android:textAllCaps="false" />

</RelativeLayout>

TableLayout表格布局

image.png

从图中可以看出TableLayout布局默认是垂直方向进行布局的,而且宽度是填充屏幕的

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blue">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第1个" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第2个" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第3个" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第4个" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第5个" />

</TableLayout>

和TableRow结合使用,发现超出屏幕部分会被截掉


image.png
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/blue">

    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第1个" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第2个" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第3个" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第4个" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第5个" />

    </TableRow>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第1个" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第2个" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第3个" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第4个" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第5个" />

</TableLayout>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:collapseColumns="0,1"
    android:stretchColumns="2,3"
    android:background="@color/blue">

    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第1个" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第2个" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第3个" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第4个" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第5个" />

    </TableRow>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第1个" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第2个" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第3个" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第4个" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第5个" />

</TableLayout>

GridLayout 网格布局

image.png

columnCount设置一行显示几列属性(用户水平布局)
rowCount 设置一列显示几行属性(用于垂直布局)

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="3"
    android:background="@color/green">

    <Button
        android:id="@+id/grid_btn_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第1个" />

    <Button
        android:id="@+id/grid_btn_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第2个" />

    <Button
        android:id="@+id/grid_btn_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        android:text="第3个" />

    <Button
        android:id="@+id/grid_btn_4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第4个" />

    <Button
        android:id="@+id/grid_btn_5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第5个" />

</GridLayout>

设置子视图:layout_row 显示在第几行;layout_column显示在第几列;layout_columnSpan横跨几列显示。
以及columnWeight列权重显示, rowWeight 行权重显示


image.png
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/green">

    <Button
        android:id="@+id/grid_btn_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第1个" />

    <Button
        android:id="@+id/grid_btn_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="1"
        android:layout_column="0"
        android:layout_columnSpan="3"
        android:layout_gravity="fill"
        android:text="第2个" />

    <Button
        android:id="@+id/grid_btn_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="0"
        android:layout_column="1"
        android:text="第3个" />

    <Button
        android:id="@+id/grid_btn_4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:text="第4个" />
<!--这里有一个问题 设置最后一个权重 rowWeight 没有作用?-->
    <Button
        android:id="@+id/grid_btn_5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第5个" />

</GridLayout>

FrameLayout帧布局

FrameLayout布局两个特有属性 foreground 前景色 foregroundGravity前景色位置


image.png
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/red"
    android:foreground="@drawable/ic_baseline_person_24"
    android:foregroundGravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:text="FrameLayout学习 1"
        android:textAllCaps="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:text="FrameLayout学习 2"
        android:textAllCaps="false"
        android:layout_gravity="right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</FrameLayout>

ConstraintLayout约束布局

1、使用ConstraintLayout设置 imageview 的时候需要注意,设置 srcCompat图片资源的时候,预览没问题,但是运行的时候就会看不到图片资源。因为使用ConstraintLayout,代码中使用的是 imageview,但是设置图片资源用的是 app:srcCompat。我们知道 imageview 设置图片资源的时候用的是 android:src 所以导致了加载不出来。
2、从 Android23.3 开始支持向量 drawable,只能通过 app:srcCompat进行加载了。
3、解决方案把 imageview 换成AppCompatImageView;或者 activity 继承AppCompatActivity


image.png
image.png
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.133"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.067" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="90dp"
        android:text="Button"
        app:layout_constraintStart_toStartOf="@+id/button"
        app:layout_constraintTop_toBottomOf="@+id/button" />

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/imageView"
        android:layout_width="94dp"
        android:layout_height="102dp"
        android:layout_marginStart="124dp"
        android:adjustViewBounds="true"
        android:background="@color/purple_700"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/button2"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.246"
        app:srcCompat="@drawable/pic1"
        tools:visibility="visible" />

</androidx.constraintlayout.widget.ConstraintLayout>
上一篇下一篇

猜你喜欢

热点阅读