Android-佈局管理-線性佈局

    線性佈局是將放入其中的組件按照垂直水平方向來佈局。每一行或每一列中只能放一個組件,而且Android的線性佈局不會換行,當組件排列到窗口邊緣後將不會被顯示出來。基本的語法格式以下:java

<LinearLayout xmlns:android = "http://scshemas.android.com/apk/res/android"
   屬性列表
   >
</LinearLayout>

    在線性佈局管理器中,經常使用的屬性包括android:orientation、android:gravity、android:layout_width、android:layout_height、android:idandroid:background。其中,前兩個屬性是屬於線性佈局管理器支持的屬性,後面四個是android.view.View和android.view.ViewGroup支持的屬性,下面進行詳細介紹。android

一、android:orientation

    android:orientation屬性用於設置佈局管理器內組件的排列方式,其可選值爲horizontalvertical,默認值爲vertical。bash

  • horizontal  表示水平排序;
  • vertical       表示垂直排列;

二、android:gravity

    android:gravity屬性用戶設置佈局管理器內組件的對齊方式,其可選值包括以下:佈局

top 將對象放在其容器的頂部,不改變其大小.
bottom 將對象放在其容器的底部,不改變其大小.
left 將對象放在其容器的左側,不改變其大小.
right 將對象放在其容器的右側,不改變其大小.
center_vertical 將對象縱向居中,不改變其大小. 
垂直對齊方式:垂直方向上居中對齊。
fill_vertical 必要的時候增長對象的縱向大小,以徹底充滿其容器. 
垂直方向填充
center_horizontal 將對象橫向居中,不改變其大小. 
水平對齊方式:水平方向上居中對齊
fill_horizontal 必要的時候增長對象的橫向大小,以徹底充滿其容器. 
水平方向填充
center 將對象橫縱居中,不改變其大小.
fill 必要的時候增長對象的橫縱向大小,以徹底充滿其容器.
clip_vertical

附加選項,用於按照容器的邊來剪切對象的頂部和/或底部的內容. 剪切基於其縱向對齊設置:頂部對齊時,剪切底部;底部對齊時剪切頂部;除此以外剪切頂部和底部.spa

垂直方向裁剪code

clip_horizontal

附加選項,用於按照容器的邊來剪切對象的左側和/或右側的內容. 剪切基於其橫向對齊設置:左側對齊時,剪切右側;右側對齊時剪切左側;除此以外剪切左側和右側.xml

水平方向裁剪對象

    這些屬性值也能夠同時指定,各屬性值之間用豎線隔開。例如,要指定組件靠右下角對齊,可使用屬性值right|bottom排序

:Android:layout_gravity和android:gravity的使用區別;圖片

三、android:layout_width

    本屬性用於設置組件的基本寬度,其可選值包括以下:

  • fill_parent            表示該組件的寬度與父類容器的寬度相同;
  • match_parent      與fill_parent相同,從Android2.2開始推薦使用;
  • wrap_content      表示該組件的寬度剛好能包裹它的內容;
說明:android:layout_width屬性是ViewGroup.LayoutParams所支持的XML屬性,對於其餘的佈局管理器一樣適用

注:android:layout_width和android:layout_weight關係

      android:layout_width和android:width的區別

四、android:layout_height

    本屬性用於設置組件的基本高度,其可選值包括以下:

  • fill_parent            表示該組件的寬度與父類容器的寬度相同;
  • match_parent      與fill_parent相同,從Android2.2開始推薦使用;
  • wrap_content      表示該組件的寬度剛好能包裹它的內容;
說明:android:layout_width屬性是ViewGroup.LayoutParams所支持的XML屬性,對於其餘的佈局管理器一樣適用

注:android:layout_height和android:height的區別

五、android:id

    本屬性用於爲當前組件指定一個id屬性,在Java代碼中能夠應用該屬性單獨引用這個組件。爲組件指定id屬性後,在R.java文件中,會自動派生一個對應的屬性,在Java代碼中,能夠經過findViewById()方法來獲取它。

六、android:background

    本屬性用於爲組件設置背景,能夠是背景圖片,也能夠是背景顏色。爲組件指定背景圖片時,能夠將準備好的背景圖片複製到目錄下,而後使用下面的代碼進行設置:

android:background="@drawable/background"

若是想指定背景顏色,可使用顏色值。代碼以下:

android:background="#FFFFFFFF"

七、樣例代碼清單

  • 垂直列表
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/buttonCeshi1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登陸1"/>
    <Button
        android:id="@+id/buttonCeshi2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登陸2"/>
    <Button
        android:id="@+id/buttonCeshi3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登陸3"/>
    <Button
        android:id="@+id/buttonCeshi4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登陸4"/>
    <Button
        android:id="@+id/buttonCeshi5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登陸5"/>

</LinearLayout>

效果以下:

  • 水平列表
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/buttonCeshi1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登陸1"/>
    <Button
        android:id="@+id/buttonCeshi2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登陸2"/>
    <Button
        android:id="@+id/buttonCeshi3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登陸3"/>
    <Button
        android:id="@+id/buttonCeshi4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登陸4"/>

</LinearLayout>

效果以下:

相關文章
相關標籤/搜索