4.1 线性布局

添加三个线性布局,按照1:2:3比例垂直瓜分手机屏幕。将约束布局改为线性布局,删掉默认的标签。可以搭配形成很多种对齐方式)2、在布局资源中写入代码。将约束布局改为线性布局。
摘要由CSDN通过智能技术生成

一、线性布局

  • 线性布局(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"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值