美文网首页
LinearLayout

LinearLayout

作者: 撕裂的我 | 来源:发表于2017-10-04 21:35 被阅读0次

1.0 基础知识

  • 基本介绍
    LinearLayout叫做线性布局,顾名思义就是将其中的控件在线性方向上排列,排列方式分为水平方向(horizontal)垂直方向(vertical),可以通过orientation来设置。

注意事项

  • 1 LinearLayout如果排列方向是horizontal时,内部的控件的宽度就不能设置为match_parent,如果设置为match_parent该控件就会占满整个屏幕,水平排列的其他控件就没有空间去排列;同理,排列方式为vertical时,内部的控件的高度属性就不能设置为match_parent,不然该控件的高度就会占满屏幕。
  • 2 使用android:layout_gravity时,当LinearLayout的排列方式是horizontal时,只有垂直方向的排列方式才会生效,以为此时水平方向上的长度是不固定的,每添加一个控件都会发生改变,因此无发设置水平方向的对齐方式。同理,为vertical时是无法设置垂直方向的对齐方式,因为高度是不固定的。
  • 3 多用weight来进行布局,还可以很好的解决适配问题。

2.0 实战

2.1 实现三段式布局

图一

用LinearLayout可以实现上面三段式效果,重点是title使用权重,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="BACK"/>
    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"  //**这里是重点**
        android:layout_height="wrap_content"
        android:text="title"
        android:gravity="center"
        android:textSize="20sp"/>
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:text="edit"/>
</LinearLayout>

参考文章:《第一行代码》

相关文章

网友评论

      本文标题:LinearLayout

      本文链接:https://www.haomeiwen.com/subject/wjncyxtx.html