android開發之布局

來源:互聯網
上載者:User

標籤:

     目前用得比較多的布局有線性布局、相對布局以及幀布局。

線性布局

     特點:組件一個挨著一個排列,通過設定,可以是橫向排列或者是縱向排列。

 

幀布局

     特點:一個組件一幀,疊加在一起,起始位置都在布局的左上方,可以通過設定控制組件位置。

     如下面的xml檔案,定義了6個TextView,每一個TextView都設定在布局的正中間,每一個顏色都不一樣,並且越在外層,面積越小,就形成了如下效果。注意,在幀布局中,越後聲明的控制項在越上層,如果a控制項在b控制項之前聲明,那麼a控制項會被b控制項覆蓋。

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><!-- 依次定義6個TextView,先定義的TextView位於底層    後定義的TextView位於上層 --><TextView android:id="@+id/view01"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_gravity="center"    android:width="320px"    android:height="320px"    android:background="#f00"    /><TextView android:id="@+id/view02"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_gravity="center"    android:width="280px"    android:height="280px"    android:background="#0f0"    /><TextView android:id="@+id/view03"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_gravity="center"    android:width="240px"    android:height="240px"    android:background="#00f"    /><TextView android:id="@+id/view04"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_gravity="center"    android:width="200px"    android:height="200px"    android:background="#ff0"    /><TextView android:id="@+id/view05"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_gravity="center"    android:width="160px"    android:height="160px"    android:background="#f0f"    /><TextView android:id="@+id/view06"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_gravity="center"    android:width="120px"    android:height="120px"    android:background="#0ff"    /></FrameLayout>

   效果:

圖1

 

相對布局

     特點:組件位置可以通過相對兄弟組件、父容器來決定。

     如下面的代碼,其效果2如示,第一個TextView通過android:layout_centerInParent="true" 使其位於布局正中間,而接下來4個TextView則通過描述其相對於第一個TextView位置來設定其位置,從而形成圖2的效果。

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><!-- 定義該組件位於父容器中間 -->    <TextView     android:id="@+id/view01"    android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:background="@drawable/leaf"    android:layout_centerInParent="true"    /><!-- 定義該組件位於view01組件的上方 --><TextView     android:id="@+id/view02"    android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:background="@drawable/leaf"    android:layout_above="@id/view01"    android:layout_alignLeft="@id/view01"    /><!-- 定義該組件位於view01組件的下方 --><TextView     android:id="@+id/view03"    android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:background="@drawable/leaf"    android:layout_below="@id/view01"    android:layout_alignLeft="@id/view01"    /><!-- 定義該組件位於view01組件的左邊 --><TextView     android:id="@+id/view04"    android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:background="@drawable/leaf"    android:layout_toLeftOf="@id/view01"    android:layout_alignTop="@id/view01"    /><!-- 定義該組件位於view01組件的右邊 --><TextView     android:id="@+id/view05"    android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:background="@drawable/leaf"    android:layout_toRightOf="@id/view01"    android:layout_alignTop="@id/view01"    />        </RelativeLayout>

 

圖2

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.