Android开发学习笔记(四)线性布局LinearLayout

线性布局(LinearLayout)包含很多知识点,放上效果图和源码来具体分析。
效果图:
在这里插入图片描述
源码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="300dp"
    android:layout_height="300dp"
    android:orientation="horizontal"
    android:layout_gravity="center"
    android:gravity="center|top"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮1"
        android:layout_weight="1">

    </Button>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮2"
        android:layout_weight="1">
    </Button>

    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮3"
        android:layout_weight="1">

    </Button>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮4">
    </Button>
    </LinearLayout>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮5">

    </Button>

</LinearLayout>

android:orientation是线性的排布方式,分为横向horizontal,纵向vertical
android:layout_gravity是整个LinearLayout在父容器中的相对位置,很多属性用的时候去查,比如center就是居中
android:gravity是其中内容在容器中的相对位置
android:layout_weight是权重,权重决定该元素在该布局中所占空间的比例,如果权重一样则均分空间
嵌套布局:在LinearLayout中嵌套一层即可,如图的按钮3效果所示

猜你喜欢

转载自blog.csdn.net/qq1198768105/article/details/113820565