2.2线性布局

一、线性布局

  • 线性布局(LinearLayout)是一种比较常用且简单的布局方式。在这种布局中,所有的子元素都是按照垂直或水平的顺序排列在界面上。如果是垂直排列,每个子元素占一行,如果是水平排列,则每个子元素占一列。线性布局可以支持布局样式嵌套实现复杂的布局样式。

1、常用属性

属性含义
layout_height高度,单位:dp (wrap_content(根据内容确定高度), match_parent
layout_weight宽度,单位:dp (wrap_content, match_parent)
orietation方向(vertical,horizontal)
gravity对齐方式(left, right, center, top, bottom…)
background背景(颜色[color]、图片[drawable]、选择器[selector])
weight比重(用于瓜分手机屏幕)
padding内边距 (paddingLeft, paddingRight, paddingTop, paddingBottom)
margin外边距 (marginLeft, marginRight, marginTop, marginBottom)

二、案例(线性布局属性)

1、创建安卓应用

  • 基于Empty Activity模板创建安卓应用 - LinearLayoutDemo

2、主布局资源文件

  • 将约束布局改为线性布局,删掉默认的标签
    在这里插入图片描述

  • 添加两个按钮

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <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"/>
</LinearLayout>

3、字符串资源文件

<resources>
    <string name="app_name">线性布局演示</string>
</resources>

4、预览效果

  • 查看效果,发现两个按钮水平摆放,在窗口左上角
    在这里插入图片描述

5、设置布局属性,查看效果

(1)设置线性布局方向

  • orientation属性
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"

    tools:context=".MainActivity">
    <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"/>

</LinearLayout>

在这里插入图片描述

(2)设置线性布局内边距

  • padding (paddingTop, paddingBottom, paddingLeft, paddingRight)
    在这里插入图片描述
    在这里插入图片描述

(3)设置线性布局对齐方式

  • gravity (left、center、right、top、bottom可以搭配形成很多种对齐方式)
    例如:居中
    在这里插入图片描述
    在这里插入图片描述

  • 还可以设置联合对齐方式(如:gravity:“center|top” :上中)
    在这里插入图片描述
    在这里插入图片描述

(4)设置线性布局背景

  • 设置背景颜色
    在这里插入图片描述
    在这里插入图片描述

  • 设置背景图片

  • 1、在drawable里添加图片
    在这里插入图片描述

  • 2、在布局资源中写入代码
    在这里插入图片描述

  • 3、效果
    在这里插入图片描述

(5)实现边框渐变色效果

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


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    //设置圆角
    <corners android:radius="10dp"/>
    //填充颜色
    <solid android:color="#ffaaff"/>
    //边框色、宽度
    <stroke android:width="1dp" android:color="#aa5555"/>
    <gradient android:startColor="#ffff00"
        android:centerColor="#ff3388"
        android:endColor="#ffaaff"/>
</shape>

在这里插入图片描述

三、线性布局嵌套

1、创建安卓应用

  • 基于Empty Activity创建安卓应用 - NestedLinearLayout

2、准备图片素材

  • 将三张小图片拷贝到res/drawable目录
    在这里插入图片描述

3、主布局资源文件

  • 将约束布局改为线性布局
    在这里插入图片描述

  • 添加三个线性布局,按照1:2:3比例垂直瓜分手机屏幕
    在这里插入图片描述

4、字符串资源文件

<resources>
    <string name="app_name">线性布局嵌套演示</string>
</resources>

5、查看预览效果在这里插入图片描述

6、修改布局,查看效果

  • 在第一个布局添加一张图片和两个按钮
  • 在第二个布局里添加一个横向线性布局,里面添加四个按钮,以及在第二个布局里添加一个编辑框
  • 在第三个布局中添加一个图片
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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="0dp"
        android:gravity="center"
        android:background="#ccffcc"
        android:layout_weight="1">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <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"/>

        </LinearLayout>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/img01"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:gravity="center"
        android:background="#3399dd"
        android:orientation="vertical"
        android:layout_weight="2"
        android:layout_height="0dp"
        android:padding="20dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
            <Button
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:text="按钮1"/>
            <Button
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:text="按钮2"/>
            <Button
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:text="按钮3"/>
            <Button
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="按钮4"/>

        </LinearLayout>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#aabbcc"
            android:lines="3"
            android:layout_marginTop="10dp"
            android:text="演示线性布局嵌套,横向和纵向的布局可以相互嵌套,形成比较复杂的局面" />

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:gravity="center"
        android:background="#ff00ff"
        android:layout_weight="3"
        android:layout_height="0dp">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/img03"/>

    </LinearLayout>

</LinearLayout>
  • 效果
    在这里插入图片描述
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值