安卓開發學習筆記(八):線性布局


 

線性布局是Android中較為常用的布局方式,使用LinearLayout標簽。線性布局主要有兩種形式,一種是水平線性布局,一種是垂直線性布局。需要注意的是Android的線性布局不會換行,當組件一個挨着一個地排列到頭之后,剩下的組件將不會被顯示出來。

 

下表顯示了LinearLayout支持的常用XML屬性及相關方法的說明。

 

 

LinearLayout 包含的所有子元素都受 LinearLayout.LayoutParams 控制,因此 LinearLayout包含的子元素可以額外指定如如下屬性。

 

android:layout_gravity:指定該子元素在LinearLayout中的對齊方式。

 

android:layout_weight:指定該子元素在LinearLayout中所占的權重。下面我們來看看具體如何進行線性布局:

 

一.線性布局的控件在整個控件最高處

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/fruit_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/fruit_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
//這里用這個將textview寫到了整個線性布局的最下面上方
 android:layout_marginLeft="10dp"/> </LinearLayout>

二.線性布局的控件在整個控件的中間

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/fruit_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/fruit_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
//這里用這個將textview寫到了整個線性布局的最中間
 android:layout_marginLeft="10dp"/> </LinearLayout>

三.線性布局的控件在整個控件的最下方

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/fruit_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/fruit_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"//這里用這個將textview寫到了整個線性布局的最下面
        android:layout_marginLeft="10dp"/>


</LinearLayout>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM