Android佈局--LinearLayout

LinearLayout是線性佈局控件,它包含的子控件將以橫向或豎向的方式排列。android

1.LinearLayout的經常使用屬性佈局

android:orientation="vertical"

該屬性決定其餘子類控件的排布方式(vertical垂直;horizontal水平)code

android:gravity="center"

該屬性決定其餘子類的xy的位置xml

經常使用屬性值:it

  1. >center_vertical  -- 垂直居中
  2. >center_horizontal -- 水平居中
  3. >center  -- 水平垂直都居中
  4. >right -- 右邊
  5. >left -- 左邊
  6. >bottom -- 下面

2.子類控件在LinearLayout中經常使用到的屬性io

android:layout_gravity="bottom"  ----指自己在當前父容器的XY的一個位置

android:layout_weight="1"        ----指自己控件佔當前父容器的一個比例

實例:class

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.demo4.MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="Button1"/>

    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button2"/>

</LinearLayout>

效果以下:容器