Android入门--UI开发--5种常见布局简介(LinearLayout篇)

9 篇文章 0 订阅
8 篇文章 0 订阅

线性布局在(LinearLayout)在实际开发中比较常用,它主要以水平和垂直方式来显示界面中的控件。当空间水平水平排列时,显示顺序依次为左到右;当控件垂直排列时,显示顺序依次为从上到下。

这里我们简单创建一个线性布局在(LinearLayout)实践学习一下。

 

 这里layout_widthlayout_height分别表示长和宽,这个简单介绍下我们经常用的两个参数

match_parent(填充父窗体由父容器大小决定控件大小)
wrap_content(包裹内容让当前控件根据控件内容大小自动伸缩)

在线性布局中,还有一个非常重要的属性 orientation,用于控制控件的排列方向,该属性有vertical和horizontal(默认)两个值。

vertical表示线性布局垂直显示:

<?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="vertical">//表示线性垂直布局
    //orientation这里有两个属性,(horizontal)表示线性布局水平显示,(vertical)表示线性垂直布局

    <Button
        android:id="@+id/btn_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮一" />

    <Button
        android:id="@+id/btn_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮二" />

    <Button
        android:id="@+id/btn_three"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮三" />

</LinearLayout>

 

horizontal表示线性布局水平显示:

<?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">//表示线性布局水平显示
    //orientation这里有两个属性,(horizontal)表示线性布局水平显示,(vertical)表示线性垂直布局

    <Button
        android:id="@+id/btn_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮一" />

    <Button
        android:id="@+id/btn_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮二" />

    <Button
        android:id="@+id/btn_three"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮三" />

</LinearLayout>

 通过观察上图我们可以发现,当设置orientation="horizontal"//线性布局水平显示时,会出现3个Button未占满一行,右侧留有空白区域的情况,这样既不美观又浪费空间。此时利用layout_weight属性可以完美解决这个问题,通过比例调整布局中所有控件的大小,在进行屏幕适配起到关键作用,实践代码如下:

<?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">//(默认)表示线性布局水平显示

    <Button
        android:id="@+id/btn_one"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="按钮一" />

    <Button
        android:id="@+id/btn_two"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="按钮二" />

    <Button
        android:id="@+id/btn_three"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="按钮三" />

</LinearLayout>

通过代码块和图片我们观察可以发现,layout_weight是通过所设置的数值大小来决定权重比的分配,这里以“按钮一”为例,计算方式是1/(1+1+2)=1/4,其中1自身占比,(1+1+2)3个按钮占比总和,因此“按钮一”占据了1/4的位置。

     需要注意的是,在上述代码中线性布局layout_width的属性值不可用设为wrap_content,因为LinearLayout的优先级比Button高,所以Button标签中layout_weight属性也会失去作用。当Button标签中使用layout_weight时,控件宽度不再由layout_width来决定,所以指定0dp不会影响效果,这样写也是一种规范。

PS:新手上路,后续更新肯定会补充更多详细的,至于什么时候更看心情吧(手动狗头)。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值