2月7日线性布局LinearLayout

1.线性布局在水平方向居中排列并靠近底部
在activity_main.xml文件中加入如下代码行

    android:gravity="center_horizontal|bottom"

在这里插入图片描述
2.子线性布局水平居中排列
红色方格水平居中排列

    <LinearLayout
        android:layout_gravity="center_horizontal"
        android:background="#ff0000"
        android:layout_width="100dp"
        android:layout_height="100dp"/>

在这里插入图片描述
3.用图片设置组件之间的分割线
两行代码一起作用

    android:divider="@drawable/divider"
    android:showDividers="beginning"

在这里插入图片描述

分割线与左右两边间距(布局与分割线一起移动

    android:padding="100dp"

在这里插入图片描述
分割线与左右两边间距(只有分割线移动
在这里插入图片描述
4.等比例分配权重
例如设置绿色的权重为1,分配的是剩余空间(保留下红色和蓝色剩下的最大的空间)

        android:layout_weight="1"

在这里插入图片描述
同时设置权重

    <LinearLayout
        android:layout_weight="1"
        android:background="#ff0000"
        android:layout_width="100dp"
        android:layout_height="match_parent"/>

    <LinearLayout
        android:layout_weight="1"
        android:background="#00ff00"
        android:layout_width="100dp"
        android:layout_height="match_parent" />

    <LinearLayout
        android:layout_weight="1"
        android:background="#0000ff"
        android:layout_width="100dp"
        android:layout_height="match_parent"/>

在这里插入图片描述
权重计算方法:例如三个布局的高全部设置为match_parent,去掉分割线

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:showDividers="middle"
    android:dividerPadding="100dp"
    android:orientation="vertical">

    <LinearLayout
        android:layout_weight="2"
        android:background="#ff0000"
        android:layout_width="100dp"
        android:layout_height="match_parent"/>

    <LinearLayout
        android:layout_weight="1"
        android:background="#00ff00"
        android:layout_width="100dp"
        android:layout_height="match_parent" />

    <LinearLayout
        android:layout_weight="1"
        android:background="#0000ff"
        android:layout_width="100dp"
        android:layout_height="match_parent"/>



</LinearLayout>

在这里插入图片描述

此时,权重大的红色布局反而不见了,剩下的等分
计算方式:有一个布局中红绿蓝三个布局分,1-3=-2
红色占比为权重的四分之二,权重的计算方式为1+剩余空间,
则1-2*(2/4)=0

所以一般设置高为0dp

    <LinearLayout
        android:layout_weight="2"
        android:background="#ff0000"
        android:layout_width="100dp"
        android:layout_height="0dp"/>

    <LinearLayout
        android:layout_weight="1"
        android:background="#00ff00"
        android:layout_width="100dp"
        android:layout_height="0dp" />

    <LinearLayout
        android:layout_weight="1"
        android:background="#0000ff"
        android:layout_width="100dp"
        android:layout_height="0dp"/>

在这里插入图片描述
5.用View设置组件之间的分割线

    <View
        android:background="@color/black"
        android:layout_width="match_parent"
        android:layout_height="1dp"/>

效果图
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值