Android学习记录9————LinearLayout(线性布局)

布局的使用

在一个丰富的界面中总是由很多个控件组成,这时我们就需要借助布局来对这些控件进行相应调整,从而编写出精美的界面。当然布局出来放置控件外,也可以放置布局,也就是嵌套。通过嵌套我们就可以完成一些相对复杂的界面。

LinearLayout(线性布局)

LinearLayout也就是线性布局,是开发当中非常常用的一种布局。在这个布局当中,所有的控件以及内布都是线性方向上依次排列的。
既然是线性排列,那肯定不仅只有一个方向,它可以水平方向,也可以垂直方向,只需要我们修改LinearLayout的orientation属性。vertical表示的是垂直方向,horizontal表示的是水平方向。

<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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button1"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button2"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button3"/>

</LinearLayout>

我把orientation属性设置为horizontal,也就是水平排序。
在这里插入图片描述
LinearLayout还有一个非常重要的属性,layout_weight权重。他在我们手机屏幕的适配性方面起到了非常重要的作用。下面举一个简单的例子。

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

    <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="文本内容提示"/>

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="发送"/>

</LinearLayout>

这里是一个水平布局,布局里面有一个EditText和一个Button,宽度都是0dp,设置他们的权重layout_weight都是1,此时EditText和Button就会在水平方向平分父布局的宽度。
在这里插入图片描述
如果我们不想他们以1:1的方式平分,我们可以修改他们的权重,比如EditText的layout_weight改为3,button的改为2,此时他们的宽度占比就是3:2。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值