Android入門學習_代碼常用布局

來源:互聯網
上載者:User

 

1、線性布局 LinearLayout:
       線性布局是所有布局中最常用的類之一,也是RadioGroup, TabWidget, TableLayout, TableRow, ZoomControls類的父類。LinearLayout可以讓它的子項目垂直或水平的方式排成一行(不設定方向的時候預設按照垂直方向排列)。

舉個例子:

java代碼:
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

>

<!--

android:id —— 為控制項指定相應的ID

android:text —— 指定控制項當中顯示的文字,需要注意的是,這裡盡量使用strings.xml檔案當中的字串

android:grivity —— 指定控制項的基本位置,比如說置中,居右等位置

android:textSize —— 指定控制項當中字型的大小

android:background —— 指定該控制項所使用的背景色,RGB命名法

android:width —— 指定控制項的寬度

android:height —— 指定控制項的高度

android:padding* —— 指定控制項的內邊距,也就是說控制項當中的內容

android:layout_weight —— 控制項之間的權重比

android:sigleLine —— 如果設定為真的話,則將控制項的內容在同一行當中進行顯示

-->

<TextView

android:id="@+id/firstText"

android:text="第一行一行一行一行一行一行一行一行一行一行"

android:gravity="center_vertical"

android:textSize="35pt"

android:background="#aa0000"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:paddingLeft="10dip"

android:paddingTop="20dip"

android:paddingRight="30dip"

android:paddingBottom="40dip"

android:layout_weight="1"

android:singleLine="true"/>

<TextView

android:id="@+id/secondText"

android:text="第二行"

android:gravity="center_vertical"

android:textSize="15pt"

android:background="#0000aa"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="1"/>

</LinearLayout>
       2、相對布局 RelativeLayout
       相對布局 RelativeLayout 允許子項目指定它們相對於其父元素或兄弟元素的位置,這是實際布局中最常用的布局方式之一。它靈活性大很多,當然屬性也多,操作難度也大,屬性之間產生衝突的的可能性也大,使用相對布局時要多做些測試。

舉個例子:

java代碼:
<?xml version="1.0" encoding="utf-8"?>

<!--

android:layout_above 將該控制項的底部至於給定ID的控制項之上

android:layout_below 將該控制項的頂部至於給定ID的控制項之下

android:layout_toLeftOf 將該控制項的右邊緣和給定ID的控制項的左邊緣對齊

android:layout_toRightOf 將該控制項的左邊緣和給定ID的控制項的右邊緣對齊

 

android:layout_alignBaseline 該控制項的baseline和給定ID的控制項的baseline對齊

android:layout_alignBottom 將該控制項的底部邊緣與給定ID控制項的底部邊緣

android:layout_alignLeft 將該控制項的左邊緣與給定ID控制項的左邊緣對齊

android:layout_alignRight 將該控制項的右邊緣與給定ID控制項的右邊緣對齊

android:layout_alignTop 將給定控制項的頂部邊緣與給定ID控制項的頂部對齊

 

 

android:alignParentBottom 如果該值為true,則將該控制項的底部和父控制項的底部對齊

android:layout_alignParentLeft 如果該值為true,則將該控制項的左邊與父控制項的左邊對齊

android:layout_alignParentRight 如果該值為true,則將該控制項的右邊與父控制項的右邊對齊

android:layout_alignParentTop 如果該值為true,則將空間的頂部與父控制項的頂部對齊

 

android:layout_centerHorizontal 如果值為真,該控制項將被至於水平方向的中央

android:layout_centerInParent 如果值為真,該控制項將被至於父控制項水平方向和垂直方向的中央

android:layout_centerVertical 如果值為真,該控制項將被至於垂直方向的中央

-->

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<TextView

android:id="@+id/label"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="Type here:" />

<EditText

android:id="@+id/entry"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="@android:drawable/editbox_background"

android:layout_below="@id/label" />

<Button android:id="@+id/ok"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/entry"

android:layout_alignParentRight="true"

android:layout_marginLeft="10dip"

android:text="OK" />

<Button android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_toLeftOf="@id/ok"

android:layout_alignTop="@id/ok"

android:text="Cancel" />

</RelativeLayout>
       3、表單布局 TableLayout
       和TableRow配合使用,和HTML裡的Table相似。

舉個例子:

java代碼:

<?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:stretchColumns="1">

<TableRow>

 

<TextView

android:layout_column="1"

android:text="開啟..."

android:padding="3dip" />

 

<TextView

android:text="Ctrl-O"

android:gravity="right"

android:padding="3dip" />

</TableRow>

<TableRow>

 

 

<TextView

android:layout_column="1"

android:text="儲存..."

android:padding="3dip" />

 

<TextView

android:text="Ctrl-S"

android:gravity="right"

android:padding="3dip" />

</TableRow>

 

<TableRow>

<TextView

android:layout_column="1"

android:text="另存新檔..."

android:padding="3dip" />

 

<TextView

android:text="Ctrl-Shift-S"

android:gravity="right"

android:padding="3dip" />

</TableRow>

 

<View

android:layout_height="2dip"

android:background="#FF909090" />

<TableRow>

<TextView android:text="*" android:padding="3dip" />

<TextView android:text="匯入..." android:padding="3dip" />

</TableRow>

<TableRow>

<TextView android:text="*" android:padding="3dip" />

<TextView android:text="匯出..." android:padding="3dip" />

<TextView android:text="Ctrl-E" android:gravity="right" android:padding="3dip" />

</TableRow>

 

<View android:layout_height="2dip" android:background="#FF909090" />

<TableRow>

<TextView android:layout_column="1" android:text="退出" android:padding="3dip" />

 

</TableRow>

 

</TableLayout>
 

       4、切換卡 Tabwidget
       繼承TabActivity,實現標籤的切換功能。
舉個例子:

java代碼:
<?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">

<LinearLayout

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<TabWidget

android:id="@android:id/tabs"

android:layout_width="fill_parent"

android:layout_height="wrap_content" />

<FrameLayout

android:id="@android:id/tabcontent"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<TextView

android:id="@+id/textview1"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:text="this is a tab" />

<TextView

android:id="@+id/textview2"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:text="this is another tab" />

<TextView

android:id="@+id/textview3"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:text="this is a third tab" />

</FrameLayout>

</LinearLayout>

</TabHost>
 

       其他布局:

 

       1、幀布局 FrameLayout:

       是最簡單的一個布局對象。在他裡面的的所有顯示對象愛你過都將固定在螢幕的左上方,不能指定位置,但允許有多個顯示對象,只是後一個會直接覆蓋在前一個之上顯示,會把前面的組件部分或全部擋住。

舉個例子:

 

java代碼:

<?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">

<TextView

android:text="big"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="50pt"/>

<TextView

android:text="middle"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="20pt"/>

<TextView

android:text="small"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="10pt"/>

</FrameLayout>

       2、絕對布局 AbsoluteLayout

       絕對位置AbsoluteLayout,又可以叫做座標布局,可以直接指定子項目的絕對位置,這種布局簡單直接,直觀性強,但是由於手機螢幕尺寸差別比較大,使用絕對位置的適應性會比較差。解析度不一樣的螢幕,顯示的位置也會有所不同。

舉個例子:

 

java代碼:

< ?xml version="1.0" encoding="utf-8"?>

< AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

 

< EditText

android:text="Welcome to Mr Wei's blog"

android:layout_width="fill_parent"

android:layout_height="wrap_content"/>

 

< Button

android:layout_x="250px" //設定按鈕的X座標

android:layout_y="40px" //設定按鈕的Y座標

android:layout_width="70px" //設定按鈕的寬度

android:layout_height="wrap_content"

android:text="Button"/>

 

< /AbsoluteLayout>

 

 

 

個人聲明,本人覺得這片文章很有價格,所以收藏!
原文出自:http://my.oschina.net/cathleencheng/blog/17691     

聯繫我們

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