UI 布局

LinearLayout*** 线性布局

  1. LinearLayout 线性布局
  2. 这个布局会将它所包含的控件在线性方向上一次排列,默认排列方式是横向
  3. 可以通过代码来指定排列方式
  4. android:orientation=”horizontal”
    -android:orientation=”vertical”

    需要注意的是,如果是横向排列,不能将控件的宽指定为match_parent
    这样的话,这一个控件就会将整个水平方向占满。
    同样的道理,如果是纵向排列,不能将空间的高指定为match_parent,这样一个控件会将整个纵向方向占满。

  5. android:gravity属性,是用于指定文字在控件中的对齐方式。
    android:layout_gravity是用于指定控件在布局中的对齐方式

android:layout_gravity="top"
android:layout_gravity="center_vertical"
android:layout_gravity="bottom"

需要注意的是,当布局为横向布局时,我们只能指定垂直方向的排列方向
当布局为纵向布局时,我们只能指定横向的排列方式。

比重 layout_weight
这个属性允许我们使用比例的方式来显示控件,在手机适配方面很有作用

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>

    </LinearLayout>

你会发现,这里我们将两个控件的宽度都指定为0dp,这样只是一种规范的写法,因为此时的width已经不是由android:layout_width决定,而是由layout_weight决定,我们将TextView指定为2,Button指定为1,表示在水平方向,TextView占 2/3 个父布局的宽度, Button 占 1/3 个父布局的宽度

当然我们也可以这么弄,设置Button的大小为刚刚包裹住内容就好,剩下的空间就交给TextView吧。

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值