免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
淺談Android五大布局

Android的界面是有布局和組件協(xié)同完成的,布局好比是建筑里的框架,而組件則相當(dāng)于建筑里的磚瓦。組件按照布局的要求依次排列,就組成了用戶所看見的界面。Android的五大布局分別是LinearLayout(線性布局)、FrameLayout(單幀布局)、RelativeLayout(相對(duì)布局)、AbsoluteLayout(絕對(duì)布局)和TableLayout(表格布局)。

  LinearLayout:

  LinearLayout按照垂直或者水平的順序依次排列子元素,每一個(gè)子元素都位于前一個(gè)元素之后。如果是垂直排列,那么將是一個(gè)N行單列的結(jié)構(gòu),每一行只會(huì)有一個(gè)元素,而不論這個(gè)元素的寬度為多少;如果是水平排列,那么將是一個(gè)單行N列的結(jié)構(gòu)。如果搭建兩行兩列的結(jié)構(gòu),通常的方式是先垂直排列兩個(gè)元素,每一個(gè)元素里再包含一個(gè)LinearLayout進(jìn)行水平排列。

  LinearLayout中的子元素屬性android:layout_weight生效,它用于描述該子元素在剩余空間中占有的大小比例。加入一行只有一個(gè)文本框,那么它的默認(rèn)值就為0,如果一行中有兩個(gè)等長的文本框,那么他們的android:layout_weight值可以是同為1。如果一行中有兩個(gè)不等長的文本框,那么他們的android:layout_weight值分別為1和2,那么第一個(gè)文本框?qū)⒄紦?jù)剩余空間的三分之二,第二個(gè)文本框?qū)⒄紦?jù)剩余空間中的三分之一。android:layout_weight遵循數(shù)值越小,重要度越高的原則。顯示效果如下:

 

LinearLayout
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
3 <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ff000000" android:text="@string/hello"/>
4 <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent">
5 <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ff654321" android:layout_weight="1" android:text="1"/>
6 <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#fffedcba" android:layout_weight="2" android:text="2"/>
7 </LinearLayout>
8 </LinearLayout>

 

  FrameLayout:

  FrameLayout是五大布局中最簡單的一個(gè)布局,在這個(gè)布局中,整個(gè)界面被當(dāng)成一塊空白備用區(qū)域,所有的子元素都不能被指定放置的位置,它們統(tǒng)統(tǒng)放于這塊區(qū)域的左上角,并且后面的子元素直接覆蓋在前面的子元素之上,將前面的子元素部分和全部遮擋。顯示效果如下,第一個(gè)TextView被第二個(gè)TextView完全遮擋,第三個(gè)TextView遮擋了第二個(gè)TextView的部分位置。

 

 

FrameLayout
1 <?xml version="1.0" encoding="utf-8"?>
2 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
3 <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ff000000" android:gravity="center" android:text="1"/>
4 <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ff654321" android:gravity="center" android:text="2"/>
5 <TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#fffedcba" android:gravity="center" android:text="3"/>
6 </FrameLayout>

 

  AbsoluteLayout:

  AbsoluteLayout是絕對(duì)位置布局。在此布局中的子元素的android:layout_x和android:layout_y屬性將生效,用于描述該子元素的坐標(biāo)位置。屏幕左上角為坐標(biāo)原點(diǎn)(0,0),第一個(gè)0代表橫坐標(biāo),向右移動(dòng)此值增大,第二個(gè)0代表縱坐標(biāo),向下移動(dòng),此值增大。在此布局中的子元素可以相互重疊。在實(shí)際開發(fā)中,通常不采用此布局格式,因?yàn)樗慕缑娲a過于剛性,以至于有可能不能很好的適配各種終端。顯示效果如下:

AbsoluteLayout
1 <?xml version="1.0" encoding="utf-8"?>
2 <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
3 <TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#ffffffff" android:gravity="center" android:layout_x="50dp" android:layout_y="50dp" android:text="1"/>
4 <TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#ff654321" android:gravity="center" android:layout_x="25dp" android:layout_y="25dp" android:text="2"/>
5 <TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#fffedcba" android:gravity="center" android:layout_x="125dp" android:layout_y="125dp" android:text="3"/>
6 </AbsoluteLayout>

RelativeLayout:

  RelativeLayout按照各子元素之間的位置關(guān)系完成布局。在此布局中的子元素里與位置相關(guān)的屬性將生效。例如android:layout_below, android:layout_above等。子元素就通過這些屬性和各自的ID配合指定位置關(guān)系。注意在指定位置關(guān)系時(shí),引用的ID必須在引用之前,先被定義,否則將出現(xiàn)異常。

  RelativeLayout里常用的位置屬性如下:
    android:layout_toLeftOf —— 該組件位于引用組件的左方
    android:layout_toRightOf —— 該組件位于引用組件的右方
    android:layout_above —— 該組件位于引用組件的上方
    android:layout_below —— 該組件位于引用組件的下方
       android:layout_alignParentLeft —— 該組件是否對(duì)齊父組件的左端
       android:layout_alignParentRight —— 該組件是否齊其父組件的右端
       android:layout_alignParentTop —— 該組件是否對(duì)齊父組件的頂部
       android:layout_alignParentBottom —— 該組件是否對(duì)齊父組件的底部
    android:layout_centerInParent —— 該組件是否相對(duì)于父組件居中
    android:layout_centerHorizontal —— 該組件是否橫向居中
    android:layout_centerVertical —— 該組件是否垂直居中

  RelativeLayout是Android五大布局結(jié)構(gòu)中最靈活的一種布局結(jié)構(gòu),比較適合一些復(fù)雜界面的布局。下面示例就展示這么一個(gè)情況,第一個(gè)文本框與父組件的底部對(duì)齊,第二個(gè)文本框位于第一個(gè)文本框的上方,并且第三個(gè)文本框位于第二個(gè)文本框的左方。

RelativeLayout
1 <?xml version="1.0" encoding="utf-8"?>
2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
3 <TextView android:id="@+id/text_01" android:layout_width="50dp" android:layout_height="50dp" android:background="#ffffffff" android:gravity="center" android:layout_alignParentBottom="true" android:text="1"/>
4 <TextView android:id="@+id/text_02" android:layout_width="50dp" android:layout_height="50dp" android:background="#ff654321" android:gravity="center" android:layout_above="@id/text_01" android:layout_centerHorizontal="true" android:text="2"/>
5 <TextView android:id="@+id/text_03" android:layout_width="50dp" android:layout_height="50dp" android:background="#fffedcba" android:gravity="center" android:layout_toLeftOf="@id/text_02" android:layout_above="@id/text_01" android:text="3"/>
6 </RelativeLayout>

 

TableLayout:

  TableLayout顧名思義,此布局為表格布局,適用于N行N列的布局格式。一個(gè)TableLayout由許多TableRow組成,一個(gè)TableRow就代表TableLayout中的一行。

  TableRow是LinearLayout的子類,它的android:orientation屬性值恒為horizontal,并且它的android:layout_width和android:layout_height屬性值恒為MATCH_PARENT和WRAP_CONTENT。所以它的子元素都是橫向排列,并且寬高一致的。這樣的設(shè)計(jì)使得每個(gè)TableRow里的子元素都相當(dāng)于表格中的單元格一樣。在TableRow中,單元格可以為空,但是不能跨列。

  下面示例演示了一個(gè)TableLayout的布局結(jié)構(gòu),其中第二行只有兩個(gè)單元格,而其余行都是三個(gè)單元格。

TableLayout
 1 <?xml version="1.0" encoding="utf-8"?>
2 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
3 <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
4 <TextView android:background="#ffffffff" android:gravity="center" android:padding="10dp" android:text="1"/>
5 <TextView android:background="#ff654321" android:gravity="center" android:padding="10dp" android:text="2"/>
6 <TextView android:background="#fffedcba" android:gravity="center" android:padding="10dp" android:text="3"/>
7 </TableRow>
8 <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
9 <TextView android:background="#ff654321" android:gravity="center" android:padding="10dp" android:text="2"/>
10 <TextView android:background="#fffedcba" android:gravity="center" android:padding="10dp" android:text="3"/>
11 </TableRow>
12 <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
13 <TextView android:background="#fffedcba" android:gravity="center" android:padding="10dp" android:text="3"/>
14 <TextView android:background="#ff654321" android:gravity="center" android:padding="10dp" android:text="2"/>
15 <TextView android:background="#ffffffff" android:gravity="center" android:padding="10dp" android:text="1"/>
16 </TableRow>
17 </TableLayout>

 

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
android中關(guān)于scrollview內(nèi)部組件android:layout
Android 線性布局詳解
New UI
Android 五大布局講解與應(yīng)用 – 碼農(nóng)網(wǎng)
2.1 View與ViewGroup的概念
Android LinearLayout線性布局詳解
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服