Android筆記(六) Android中的布局——線性布局

來源:互聯網
上載者:User

標籤:

         我們的軟體是由好多個介面組成的,而每個介面又由N多個控制群組成,Android中藉助布局來讓各個空間有條不紊的擺放在介面上。

         可以把布局看作是一個可以放置很多控制項的容器,它可以按照一定的規律調整控制項的位置,從而實現精美的介面。

         布局中也可以放置布局,通過多層布局的嵌套,實現比較複雜的介面。

         Android提供了四種基本布局:LinearLayout、RelativeLayout、FrameLayout、TableLayout

LinearLayout:

         LinearLayout稱為線性布局,正如其名字一樣,這個布局中的所有控制項線上性方向上依次排列。

         LinearLayout通過orientation屬性來指定排列方向是垂直還是水平,如果值為vertical,控制項為垂直排列,如果值為horizantal,控制項為水平排列

         代碼:

         activity_main.xml:

<LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity"    android:orientation="vertical"    >    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="BUTTON1"        android:id="@+id/button1"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="BUTTON2"        android:id="@+id/button2"/>    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal">        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="BUTTON3"/>        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="BUTTON4"/>    </LinearLayout></LinearLayout>

  以上代碼在模擬器中顯示為

  

  

         從上面的代碼我們可以看出,線性布局中,android:orientation屬性控制了內部控制項的布局方向,vertical為垂直布局,horizontal為水平布局,Android系統預設線性布局為水平方向(horizaontal)。

         線性布局中還有一個屬性android:gravity來控制布局內子項目的重力方向,它有以下多個參數可供選擇:

         center、  bottom、center_horizontal、center_vertital、clip_horizontal、clip_vertital、         end、fill、       fill_horizontal、fill_vertital、   left、right、start、top。

         LinearLayout還有另外一個重要的屬性——android:layout_weight,這個屬性可以讓我們使用比例的方式來指定控制項的大小,在手機螢幕適配型方面可以起到非常重要的作用。

    程式碼範例:

    activity_main.xml:

<LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity"    android:orientation="horizontal"    >    <EditText        android:layout_width="0dp"        android:layout_height="wrap_content"        android:id="@+id/usernameInput"        android:text="請輸入你的使用者名稱"        android:layout_weight="7"/>    <Button        android:layout_width="0dp"        android:layout_height="wrap_content"        android:id="@+id/loginButton"        android:text="登入"        android:layout_weight="3"/></LinearLayout>

  運行結果為:

  

  EditText占螢幕的7/10  Button占螢幕的3/10.原理很簡單,先將兩個控制項所佔比例相加為螢幕總比例,然後用各自所佔比例相除

 

 

  

Android筆記(六) Android中的布局——線性布局

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.