Android學習筆記(三)UI布局

來源:互聯網
上載者:User

一、線性布局-LinearLayout(至上而下布局)
<LinearLayout>
</LinearLayout>
如:
Java代碼 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical"    
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
 
  
 其中
android:orientation="vertical"意思為垂直
方向的線性布局,此處的"vertical"可改為"horizontal",意思是水平方向的線性布局。
android:layout_width="fill_parent"意思為這個控制項的寬度佔滿整個螢幕或者父控制項,此處的"fill_parent"可改為"wrap_parant",意思是寬度剛好包含住LinearLayout裡面的內容。
 
常用布局控制項:
 
 

android:id 

 為控制項指定相應的ID

 android:text

 指定控制項當中顯示的文字,需要注意的是,這裡盡量使用strings.xml檔案當中的字串

 android:gravity

 控制這個一個控制項的位置,比如置中、靠右、底部、上部等

 android:textSize

 指定控制項當中字型的大小

 android:background

 指定該控制項所使用的背景色,RGB命名法

 android:layout_weight

 數值為幾即該控制項佔滿整個螢幕的數值合之幾,比如當前Activity中有2個控制項,第一個控制項是android:layout_weight="2",第二個控制項是android:layout_weight="3",則,第一個控制項占整個螢幕的3/5。

 android:height

 指定控制項的高度

 android:padding*

 指定控制項的內邊距,也就是說控制項當中的內容,如android:paddingLeft="10dip"、android:paddingRight="10dip"等等。也可以直接android:padding="10dip"意思是說內邊距4面距離都為10.

 android:singleLine

 如果設定為真的話,則將控制項的所有內容在同一行當中進行顯示

 

 
二、表格版面配置-TableLayout
概述:
TableLayout多用於列表的顯示
1、把子項目放到行與列中。
2、不顯示行、列和儲存格的邊界線。
3、儲存格不能橫跨行,類似於html。
 
用例:
Java代碼 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:stretchColumns="1"> //這一條語句的意思是:是設定 TableLayout所有行的第二列為展開列。也就是說如果每行都有三列的話,剩餘的空間由第二列補齊。為0時則是第一列為展開列。目的是為了把父控制項填滿。 
 
<TableLayout> 
       <tableRow> 
             <TextView 
                 android:text="@string/row1_column1" 
                 android:background="#aa0000"    //加上背景色 
                 android:padding="3dip" 
 
         /> 
             <TextView 
                 android:text="@string/row1_column1" 
                 android:padding="3dip" 
                 android:gravity="center_horizontal"   //垂直置中顯示 
                 android:background="#00aa00" 
             / > 
             <TextView 
                 android:text="@string/row1_column2" 
                 android:gravity="right"       //居右顯示 
                 android:background="#0000aa" 
                 android:padding="3dip" 
 
         /> 
      </TablieRow> 
</TableLayout> 
 以上代碼意思為:將螢幕分為3列,一個TableRow就是一行,每一個TextView佔一個儲存格
   
 
三、嵌套布局
即多個布局嵌套使用。
如要實現以下布局:即多個LinearLayout嵌套
 


 

代碼如下:
Java代碼 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical"  //最外層使用垂直布局 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <LinearLayout 
     android:orientation="horizontal"  //第一個嵌套縱向布局 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1">  //需要注意的是嵌套的LinearLayout權重都是1,即各自占螢幕的一半。 
    </LinearLayout> 
      
    <LinearLayout 
     android:orientation="vertical"  //第二個嵌套垂直布局 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1"> 
    </LinearLayout> 
</LinearLayout> 
  四、相對布局-RelativeLayout
概念:
類似於CSS+DIV布局。
 一個控制項的位置,決定於他和周圍控制項的關係。
 
與其他控制項的區別:
相對布局是依賴與和周圍控制項的關係而決定為位置的,如將A控制項放在B控制項的下方。
 
相關屬性:
   第一類:
Java代碼 
android:layout_above 將該控制項的底部至於給定ID的控制項之上 
    android:layout_below 將該控制項的頂部至於給定ID的控制項之下 
    android:layout_toLeftOf 將該控制項的右邊緣和給定ID的控制項的左邊緣對齊 
    android:layout_toRightOf 將該控制項的左邊緣和給定ID的控制項的右邊緣對齊 
   
    android:layout_alignBaseline 該控制項的baseline和給定ID的控制項的baseline對齊 
    android:layout_alignBottom 將該控制項的底部邊緣與給定ID控制項的底部邊緣 
    android:layout_alignLeft 將該控制項的左邊緣與給定ID控制項的左邊緣對齊 
    android:layout_alignRight 將該控制項的右邊緣與給定ID控制項的右邊緣對齊 
    android:layout_alignTop 將給定控制項的頂部邊緣與給定ID控制項的頂部對齊 
   
   
    android:layout_alignParentBottom 如果該值為true,則將該控制項的底部和父控制項的底部對齊 
    android:layout_alignParentLeft 如果該值為true,則將該控制項的左邊與父控制項的左邊對齊 
    android:layout_alignParentRight 如果該值為true,則將該控制項的右邊與父控制項的右邊對齊 
    android:layout_alignParentTop 如果該值為true,則將控制項的頂部與父控制項的頂部對齊 
   
    android:layout_centerHorizontal 如果值為真,該控制項將被至於水平方向的中央 
    android:layout_centerInParent 如果值為真,該控制項將被至於父控制項水平方向和垂直方向的中央 
    android:layout_centerVertical 如果值為真,該控制項將被至於垂直方向的中央 
     
 
    通俗的理解 Padding 為內邊框,Margin 為外邊框 
 
    對應的屬性為 
 
    android:layout_marginBottom="25dip" 
    android:layout_marginLeft="10dip" 
    android:layout_marginTop="10dip" 
    android:layout_marginRight="10dip" 
    android:paddingLeft="1dip" 
    android:paddingTop="1dip" 
    android:paddingRight="1dip" 
    android:paddingBottom="1dip" 
 
    如果左右上下都是相同的設定則可以直接設定 
 
    android:layout_margin="10dip" 
    android:padding="5dip" 

 

本文出自“suuu”

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.