0
点赞
收藏
分享

微信扫一扫

2月7日线性布局LinearLayout

dsysama 2022-02-11 阅读 64

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 条评论