Android基本佈局總結

Android基本佈局

<1>Android中的佈局

LinearLayout(線性佈局)

特點:元素會一個接一個的排列。
方向:orientation 水平的:vertical 水平的:horizontal
再有就是–> wrap_content:匹配內容的大小
match_content:匹配父容器的大小,其中fill_match和他的作用是一樣的。只是fill_match出現的較早。
layout_weight:權重,控制空間所佔的比例,通常使用形式爲layout_weight+0dp結合。
來一個小Demo:

    <LinearLayou 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"
    android:orientation="vertical"
    >
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/num" />  
    </LinearLayout>  
注意:
  • 線性佈局可以相互嵌套

RelativeLayout(相對佈局)

相對佈局就是裏面的元素的位置都是以相對位置來確定的,可以是相對父容器,也可以是相對其他元素。
幾個屬性:

  • android:layout_toLeftOf、android:layout_toRightOf、android:layout_above、android:layout_below分別表示位於XXX的左方,右方,上方和下方,後面接元素的iD.
  • android:layout_alignLeft、android:layout_alignRight、android:layout_alignTop、android:layout_alignBottom分別表示和什麼元素對其。
  • android:layout_alignParentRight、android:layout_alignParentLeft、android:layout_alignParentTop、android:layout_alignParentBottom、android:layout_centerInParent、android:layout_centerHorizontal、android:layout_centerVertical分別表示相對於父容器的右,左、上、下、居中,水平居中和垂直居中。

來一個小Demo:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button 
    android:id="@+id/centerButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="中"
    />
 <Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_toLeftOf="@id/centerButton"
    android:text="左"
    />
 <Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"    
    android:layout_centerInParent="true"
    android:layout_toRightOf="@id/centerButton"
    android:text="右"
    />
 <Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_above="@id/centerButton"
    android:text="上"
    />
 <Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_below="@id/centerButton"
    android:text="下"
    />

</RelativeLayout>  

margin:外邊距,即控件與控件之間的間距
padding:內邊距,即控件中的內容和控件的距離

前面的兩種佈局要重點掌握,後面的三種佈局瞭解就行。

FrameLayout(幀佈局)

特點:一層一層的顯示。
直接上Demo:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
android:gravity="center"
android:text="TextView" />

</FrameLayout>  
注意:android:gravity=”center” 表示的是控件中的內容居中, android:layout_gravity=”center”表示控件居中

TableLayout(表格佈局)

表格佈局相當於線性佈局的子佈局,可以完全用線性佈局來實現。
Demo:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow 
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button1"/>
    <Button 
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="Button2"/>

</TableRow>

 <TableRow 
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button3"/>

</TableRow>

</TableLayout>  

表示一行。android:layout_weight表示權重

AbsoluteLayout(絕對佈局)

絕對佈局就是利用橫縱座標指定元素的具體位置。
這種佈局使得元素定位太過呆板,很少用。
Demo:

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="103dp"
    android:layout_y="52dp"
    android:text="TextView" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="115dp"
    android:layout_y="108dp"
    android:text="Button" />

</AbsoluteLayout>  
發佈了32 篇原創文章 · 獲贊 19 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章