Android 學習筆記(二): 布局概述

來源:互聯網
上載者:User

一,線性布局(LinearLayout)

以線性方向顯示它的子視圖(view)元素,垂直或水平,

android:orientation值為“vertical”垂直排列,"horizontal"即為水平排列,

 

[html] <?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
 
    <Button android:layout_width="match_parent" android:layout_height="wrap_content" 
        android:id="@+id/button1" android:text="線"  ></Button> 
    <Button android:layout_width="match_parent" android:layout_height="wrap_content" 
        android:id="@+id/button2" android:text="性"  ></Button> 
    <Button android:layout_width="match_parent" android:layout_height="wrap_content" 
        android:id="@+id/button3" android:text="布"  ></Button> 
    <Button android:layout_width="match_parent" android:layout_height="wrap_content" 
        android:id="@+id/button4" android:text="局"  ></Button> 
 
</LinearLayout> 
  


 

二,相對布局(RelativeLayout)


[html] <?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
 
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content" 
        android:id="@+id/button1" android:text="相"   
        android:layout_alignParentLeft="true"></Button> 
    <Button android:layout_width="fill_parent" android:layout_height="wrap_content" 
        android:id="@+id/button2" android:text="對"   
        android:layout_below="@+id/button1"></Button> 
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content" 
        android:id="@+id/button3" android:text="布"   
        android:layout_below="@+id/button2" 
        android:layout_toLeftOf="@+id/button4" android:layout_alignTop="@+id/button4"></Button> 
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content" 
        android:id="@+id/button4" android:text="局"   
        android:layout_below="@+id/button2" 
        android:layout_alignParentRight="true"></Button> 
 
</RelativeLayout> 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

 <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
     android:id="@+id/button1" android:text="相" 
     android:layout_alignParentLeft="true"></Button>
 <Button android:layout_width="fill_parent" android:layout_height="wrap_content"
     android:id="@+id/button2" android:text="對" 
     android:layout_below="@+id/button1"></Button>
 <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
     android:id="@+id/button3" android:text="布" 
     android:layout_below="@+id/button2"
     android:layout_toLeftOf="@+id/button4" android:layout_alignTop="@+id/button4"></Button>
 <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
     android:id="@+id/button4" android:text="局" 
     android:layout_below="@+id/button2"
     android:layout_alignParentRight="true"></Button>

</RelativeLayout>
  

 

三,表格版面配置(TableLayout)


[html] <?xml version="1.0" encoding="utf-8"?> 
<TableLayout  xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:shrinkColumns="0,1,2"> 
    <TableRow> 
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" 
            android:id="@+id/button1" android:text="表格版面配置------"  
            android:layout_column="0"></Button> 
        <Button android:layout_width="fill_parent" android:layout_height="wrap_content" 
            android:id="@+id/button2" android:text="表格版面配置------"  
            android:layout_column="1"></Button> 
    </TableRow> 
    <TableRow> 
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" 
            android:id="@+id/button3" android:text="表格版面配置------"  
            android:layout_column="1"></Button> 
    </TableRow> 
    <TableRow> 
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" 
            android:id="@+id/button4" android:text="表格版面配置------"  
            android:layout_column="2"></Button> 
    </TableRow> 
</TableLayout > 
<?xml version="1.0" encoding="utf-8"?>
<TableLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:shrinkColumns="0,1,2">
 <TableRow>
  <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
      android:id="@+id/button1" android:text="表格版面配置------" 
      android:layout_column="0"></Button>
  <Button android:layout_width="fill_parent" android:layout_height="wrap_content"
      android:id="@+id/button2" android:text="表格版面配置------" 
      android:layout_column="1"></Button>
 </TableRow>
 <TableRow>
  <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
      android:id="@+id/button3" android:text="表格版面配置------" 
      android:layout_column="1"></Button>
 </TableRow>
 <TableRow>
  <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
      android:id="@+id/button4" android:text="表格版面配置------" 
      android:layout_column="2"></Button>
 </TableRow>
</TableLayout >
  


 

四,絕對布局(AbsoluteLayout)


[html] <?xml version="1.0" encoding="utf-8"?> 
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:shrinkColumns="0,1,2"> 
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" 
            android:id="@+id/button1" android:text="絕對布局"></Button> 
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" 
            android:id="@+id/button2" android:text="絕對布局"    
            android:layout_x="160dip"></Button> 
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" 
            android:id="@+id/button3" android:text="絕對布局"    
            android:layout_y="50dip"></Button> 
 
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" 
            android:id="@+id/button4" android:text="絕對布局"    
            android:layout_x="160dip" android:layout_y="150dip"></Button> 
</AbsoluteLayout> 
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:shrinkColumns="0,1,2">
  <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
      android:id="@+id/button1" android:text="絕對布局"></Button>
  <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
      android:id="@+id/button2" android:text="絕對布局" 
      android:layout_x="160dip"></Button>
  <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
      android:id="@+id/button3" android:text="絕對布局" 
      android:layout_y="50dip"></Button>

  <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
      android:id="@+id/button4" android:text="絕對布局" 
      android:layout_x="160dip" android:layout_y="150dip"></Button>
</AbsoluteLayout>
  


摘自  柒尐狐的專欄 



聯繫我們

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