Android布局---LinearLayout(线性布局)

常用布局:LinearLayout(线性布局)、FrameLayout(帧布局)、RelativeLayout(相对布局)、GridLayout(网格布局)

布局通用属性:

属性名称功能描述
android:id设置布局的标识
android:layout_width设置布局的宽度
android:layout_height设置布局的高度
android:background设置布局的背景
android:layout_margin设置当前布局与屏幕边界或与周围控件的距离
android:padding设置当前布局与该布局中控件的距离

LinearLayout:线性布局

子View水平或垂直方向进行排列

重要属性:

orientation

排列方向属性:vertical(垂直)、horizontal(水平)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zPD6AqMH-1675940742261)(C:\Users\11726\AppData\Roaming\Typora\typora-user-images\image-20230209151521783.png)]

layout_weight

权重属性:通过所占比例指定控件的宽度或高度

取值:

  • =0:默认值,指定多少多大空间就占据多少空间
  • >0:将父视图中的可用空间进行分割,值越大权重越大,占据的比例就越大

例:在本例中将红色权重设置为1,绿色权重设置为2

<?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:orientation="horizontal">
    
<!--    红色--> 
<LinearLayout   
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF0000" 
    android:layout_weight="1"
    />
    
<!--  绿色-->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#008000 "
    android:layout_weight="2"
    />
    
</LinearLayout>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rJmZlVdl-1675940742261)(C:\Users\11726\AppData\Roaming\Typora\typora-user-images\image-20230209170527938.png)]

注:在linearlayout横向排列中设置权重时,android:layout_width应该设置为0dp,不能设置为wrap_content,否则layout_weight属性会失去作用,因为layout_width的优先级跟高(这句话不知道我的理解对不对,不对的可以在评论区里说)。同理,纵向排列中设置权重时,android:layout_height应该设置为0dp。

示例如下:
①设置layout_widthwrap_content

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

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是第一个按钮"
        android:layout_weight="1"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是第2个按钮"
        android:layout_weight="2"
        />

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ylQvFgrZ-1675940742261)(C:\Users\11726\AppData\Roaming\Typora\typora-user-images\image-20230209185425378.png)]

②设置layout_width0dp

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

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="这是第一个按钮"
        android:layout_weight="1"
        />
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="这是第二个按钮"
        android:layout_weight="2"
        />

在这里插入图片描述

gravity和layout_gravity

gravity属性是布局中内容对齐,而layout_gravity属性是布局自身对齐

取值:top、bottom、start、end、center

如图示例:

①默认状态

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

②设置gravity

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

③设置layout_gravity

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

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

昱彤*

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值