【入門篇】Android學習筆記——常用布局,android學習筆記

來源:互聯網
上載者:User

【入門篇】Android學習筆記——常用布局,android學習筆記

很多開發人員一聽說Android終端的螢幕尺寸五花八門,螢幕解析度千奇百怪,就覺得Android開發在螢幕適配方面是必定是一件頭疼的事情。因為在Android問世之前,廣大開發人員知道的UI解決方案大致分為兩類:

上面兩種方案,無論哪種方案面對片段化嚴重的Android終端,那都是一場噩夢。好在Android提供了另一套解決方案來應對嚴重的終端片段化,這就是布局和9-patch。

這裡想來說說布局,在Android SDK剛剛問世的時候,Android提供了AbsoluteLayout,FrameLayout,LinearLayout,RelativeLayout和Tablelayout五大布局來應對終端片段化問題。

但很快Android發現AbsoluteLayout是一個愚蠢的方案,在Android 1.5系統中就不再支援此布局,剩下的四個布局中,Tablelayout雖然依然被支援,但是由於Fragment以及新的TabLayout的出現,博主在此斷言,Tablelayout也命不久矣,被移除支援只是遲早的事兒。

所以,Android的五大基本布局現在只剩下三個(這裡說的是基本布局,在Android support包裡引入的新的布局不計入內),下面分別介紹一下這三個基本布局。

FrameLayout

FrameLayout應該是Android系統中最簡單的布局了,在FrameLayout中的元素,預設都是以FrameLayout控制項的坐上頂點作為基準點,一層一層的重疊起來,後加進來的元素覆蓋前面的元素。

下面先來一個示範,代碼如下:

<FrameLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <View        android:layout_width="200dp"        android:layout_height="200dp"        android:background="#ff0000"/>    <View        android:layout_width="200dp"        android:layout_height="200dp"        android:background="#000000"/>    <View        android:layout_width="200dp"        android:layout_height="200dp"        android:layout_margin="100dp"        android:background="#00ff00"/></FrameLayout>

運行結果如下:

<View android:layout_width="200dp" android:layout_height="200dp" android:layout_gravity="bottom|right" android:background="#000000"/>

  

可以看到上面代碼裡添加了android:layout_gravity屬性,並且指定了兩個值,一個為bottom,一個為right,表示這個View將被放到FrameLayout的右下角。運行結果如所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center"> <View android:layout_width="100dp" android:layout_height="100dp" android:background="#ff0000"/> <View android:layout_width="100dp" android:layout_height="100dp" android:background="#000000"/> <View android:layout_width="100dp" android:layout_height="100dp" android:background="#00ff00"/></LinearLayout>

  

在代碼中,設定了LinearLayout的方向為縱向,並且對其方式置中對齊,於是運行結果如所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <View android:id="@+id/red" android:layout_width="100dp" android:layout_height="100dp" android:background="#ff0000"/> <View android:id="@+id/black" android:layout_width="100dp" android:layout_height="100dp" android:layout_toRightOf="@id/red" android:layout_below="@id/red" android:background="#000000"/> <View android:id="@+id/green" android:layout_width="100dp" android:layout_height="100dp" android:layout_below="@id/black" android:layout_alignParentRight="true" android:background="#00ff00"/> <View android:id="@+id/gray" android:layout_width="100dp" android:layout_height="100dp" android:layout_centerInParent="true" android:background="#888888"/> <View android:id="@+id/orange" android:layout_width="100dp" android:layout_height="100dp" android:layout_toRightOf="@id/green" android:layout_below="@id/gray" android:background="#ff8800"/></RelativeLayout>

  

先分析代碼,可以看到每一個View都被設定了一個id值,分別為red,black,green,gray和orange。然後通過代碼,可以看出black位於red的右邊和下面,green位於black的下面並且靠右對齊其父元素(即RelativeLayout),gray置中對齊父元素(即RelativeLayout), orange位於green的右邊同時位於gray的下面,運行結果:

點擊這裡

 

 

==============================

 更多內容,歡迎查看本人部落格:林彥君的部落格

==============================

聯繫我們

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