Android初級開發第六講--Activity的布局

來源:互聯網
上載者:User

大家好,Android的介面主要靠布局檔案完成,只有掌握了其中的屬性與應用技藝,才能圓滿的完成任務,

所以今天我們來講講Android的布局。

自從htc1.0開始發布,安卓中有五大布局檔案,分別是:

LinearLayout 線性布局

 


RelativeLayout 相對布局

 


TableLayout 表格版面配置

 


FrameLayout 架構布局

 


AbsoluteLayout 絕對布局

 


首先布局檔案都有兩個最基本的屬性,寬和高 :

android:layout_width="fill_parent"    android:layout_height="fill_parent"

fill_parent代表充滿整個螢幕,wrap_content代表適應整個螢幕,主要是適應自己控制項的大小,

從2.2開始加入match_parent這個屬性,也代表適應整個螢幕,但這個不僅適應自己控制項大小,也會把螢幕中的空白也佔一部分,使螢幕看起來沒那麼空曠,是個比較好用的屬性。但考慮到適配機型,開發人員不太熟悉,但提倡用。

——————————————————————————————————————————————-

下面是布局的詳細介紹:

 


線性布局:最普通的布局,預設子項目從左至右排列,有horizontal水平和vertical豎直排列這兩種屬性,一定要注意預設是水平排列。

 


相對布局:最好用、最常用的布局,可以使用其中的layout_alignParent和layout_toLeftOf、layout_above,再配上padding和margin等屬性(上一講有介紹),可以為所欲為的在螢幕中放控制項。

 


表格版面配置:主要在排版使用者名稱、密碼、愛好等這樣的格子時被使用,加入tableRow可以布局中jsp各種樣式的表格。

 

 

架構布局和絕對布局:前者最常用到的就是用在tabhost裡,以下是標準選項卡布局下的部分代碼


[html]
<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/backgrounds" 
    android:orientation="vertical" > 
    <LinearLayout> 
        <FrameLayout    android:id="@android:id/tabcontent"/> 
        <TabWidget       android:id="@android:id/tabs"/> 
    </LinearLayout> 
 
</TabHost> 

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/backgrounds"
    android:orientation="vertical" >
    <LinearLayout>
        <FrameLayout    android:id="@android:id/tabcontent"/>
        <TabWidget       android:id="@android:id/tabs"/>
    </LinearLayout>

</TabHost>但在另外一個場合,它也是必用布局,那就是疊加效果。

 

 

絕對布局主要是通過layout_x,layout_y來把所有的控制項定在螢幕絕對的位置,但由於其固執,難以改變,所以它在1.5以後被棄用,開發人員用FrameLayout中的scrollX,scrollY來定義部分控制項在布局中的絕對位置,可以說從而代替了絕對布局的功能。

 


還有其中一些布局如GridLayout進行圖文混排,ScrollView,用來載入大圖片,HorizonalScrollView用來載入水平方向大的圖片等簡單的布局。

 


今天布局先講到這兒,接下來繼續講述其他知識,其中的特性在其他章節我們會一會解釋,謝謝大家!

 

聯繫我們

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