Android之LinearLayout布局嵌套-四角及中心

xml四角及中心添加组件

模型图

在这里插入图片描述

思路

LinearLayout 中添加orientation属性:设置该布局内组件的排列方式,若不添加,则默认为horizontal
LinearLayout 中添加gravity属性 :设置该布局内组件的位置
TextView 中设置gravity属性:设置该TextView组件中文本的位置

  1. 垂直方向三行,选择LinearLayout布局,设置方向垂直vertical;
    三个LinearLayout,设置layout_weight为1,平分三行

  2. 第一个LinearLayout,直接写两个TextView,都在第一行,高度自适应wrap_content;
    第一段文本的宽度自适应wrap_content就行,自动居左;
    第二段文本想要右对齐,考虑到(TextView 中设置gravity属性:设置该TextView组件中文本的位置)该原因,要将TextView组件的宽度设置成第一行剩下部分所有的宽度,所以宽度为match_parent

  3. 第二个LinearLayout,用一个LinearLayout放置四行文本(写四个TextView组件),四行文本垂直放置,所以应该将LinearLayout的orientation设置为垂直vertical
    由于LinearLayout的gravity属性可以设置子组件位置,可以将当前LinearLayout宽高设置为自适应,当前 LinearLayout的父级LinearLayout的gravity属性设置为center

  4. 第三个LinearLayout,直接写两个TextView,因为想要放在底部bottom,所以可以设置高度匹配父级match_parent,宽度设置同步骤2,第一个TextView宽度为自适应wrap_content,第二个TextView宽度设置为匹配父级占满剩下的

代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
        
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="this is the left and top"
            ></TextView>
        
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="this is the right and top"
            android:gravity="right"
            ></TextView>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center">
        
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="this is the center"
                ></TextView>
            
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="this is the center"
                ></TextView>
            
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="this is the center"
                ></TextView>
            
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="this is the center"
                ></TextView>
            </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="this is the left and bottom"
                android:gravity="bottom"
                ></TextView>
        
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="this is the right and bottom"
                android:gravity="right|bottom"
                ></TextView>
    </LinearLayout>
</LinearLayout>

修改前代码

多写了LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
            
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="this is the left and top"
                android:gravity="left"
                ></TextView>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
            
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="this is the right and top"
                android:gravity="right"
                ></TextView>
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:gravity="center">
            
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="this is the center"
                ></TextView>
                
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="this is the center"
                ></TextView>
                
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="this is the center"
                ></TextView>
                
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="this is the center"
                ></TextView>
            </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="this is the left and bottom"
                android:gravity="bottom"
                ></TextView>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="this is the right and bottom"
                android:gravity="right|bottom"
                ></TextView>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

运行结果

在这里插入图片描述

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值