关于Android的线性布局

一,线性布局

XML; android:orientation="vertical" 注意方向的显示

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    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="按钮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"
        />
</LinearLayoutr>>

android:layout_width="wrap_content"
    android:layout_height="wrap_content"注意从使用父窗,变成了包裹内容

再改

<LinearLayout android:layout_width="300"
    android:layout_height="300"

我们加上

android:layout_gravity="center",可以看到,变到了父窗口的中心

再改android:gravity="center"内部的元素,放在了自身的中间

改成android:gravity="center|top"

android:layout_weight="1"在第一个按钮添加,可以看到第一个按钮填充了剩余的位置

给按钮2也加上以后,可以看到1,2平均分配了原本1的面积

下面演示一下嵌套

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation ="horizontal"
    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="按钮2"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮2"
        />
    <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"
        />
        <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"
            />
        </LinearLayout>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮5"
            />
</LinearLayoutr>

猜你喜欢

转载自blog.csdn.net/a15929748502/article/details/110670224