自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解Android Widget組件RelativeLayout實例

移動開發(fā)
本文為Javaeye的網(wǎng)友倚風(fēng)聽雪對關(guān)于RelativeLayout的一些看法,小編覺得內(nèi)容很不錯,介紹給大家,希望大家喜歡,同時也希望讀者關(guān)注51CTO的Mobile Widget專題。

以前很少用RelativeLayout,但是這次筆者的項目用到了RelativeLayout。用起來才發(fā)現(xiàn)RelativeLayout太靈活了。很容易給人造成一些錯誤。

51CTO推薦專題:Mobile Widget

下面談?wù)劰P者的看法。

引用

  1. From Tutorials:  
  2.  
  3. If you find yourself using several nested LinearLayout groups, you may be able toreplace them with a single RelativeLayout  
  4.  

以上來自Tutorials,筆者理解的觀點是,當(dāng)有過個ViewGroup嵌套的時候,再去考慮用RelativeLayout,筆者覺得既然官方這么寫,很程度是因為,RelativeLayout太靈活了,它的靈活性給我們對UI的控制多少回造成一定影響。

曾經(jīng)有人跟筆者說過,RelativeLayout跟FrameLayout有一些相似,給人的感覺是分層的。有層的這個概念。
筆者覺得不是這樣的,是沒有層的概念的。從官方的解釋上可以看出這東西就是可以設(shè)置相對布局的一個布局而已。沒有層的概念。

先上段代碼,更直觀的看看。

Java代碼

  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <RelativeLayout    
  3.     xmlns:android="http://schemas.android.com/apk/res/android"    
  4.     android:layout_width="fill_parent"    
  5.     android:layout_height="fill_parent"    
  6.     android:background="#CCFFFF">    
  7.     <LinearLayout    
  8.         android:id="@+id/linearLayout"    
  9.         android:layout_width="fill_parent"    
  10.         android:layout_height="200dp"    
  11.         android:background="#32000033"    
  12.         android:orientation="vertical">    
  13.         <Button    
  14.             android:id="@+id/button1"    
  15.             android:layout_width="fill_parent"    
  16.             android:layout_height="wrap_content"    
  17.             android:background="#FF3300"    
  18.             android:text="Button" />    
  19.         <TextView    
  20.             android:id="@+id/textView"    
  21.             android:layout_width="fill_parent"    
  22.             android:layout_height="wrap_content"    
  23.             android:layout_weight="1"    
  24.             android:text="Base"    
  25.             android:textColor="#6633FF"    
  26.             android:gravity="center" />    
  27.         <Button    
  28.             android:id="@+id/button2"    
  29.             android:layout_width="fill_parent"    
  30.             android:layout_height="wrap_content"    
  31.             android:background="#FF3300"    
  32.             android:text="Button" />    
  33.     </LinearLayout>    
  34.     <Button    
  35.         android:id="@+id/button3"    
  36.         android:layout_width="100dp"    
  37.         android:layout_height="50dp"    
  38.         android:layout_centerInParent="true"    
  39.         android:layout_alignBottom="@id/linearLayout"    
  40.         android:text="button" />    
  41. </RelativeLayout>   

只貼xml,activity沒什么東西,就顯示一下罷了。

運行效果圖

運行效果圖

很明顯可以看出button3的下邊緣是跟lineLayout的下邊緣在一條水平線上的。

Java代碼
 

  1. android:layout_alignBottom="@id/button1"    

但是當(dāng)像上面一樣設(shè)置的時候,我們可能會是想讓button3的下邊緣跟button1的下邊緣在一個水平線,但是這些寫的效果卻不是按我們所想的顯示,如此設(shè)置根本不起作用。

運行效果圖

這其中的原因,筆者是這樣認(rèn)為的,首先,linearLayout,Button這些組件都是在android.widget這個包中的。他們是同一級別的。只是說linearLayout是一個ViewGroup可以再包含其他的View而已。不存在其他的優(yōu)先級關(guān)系。

所以,筆者的理解是,如果Button3這個控件要同其他控件產(chǎn)生相互關(guān)系的話,首先他們是要位于同一級別的。(此處說的級別不是說組件級別,而是在xml文件里面設(shè)置的級別,如:linearLayout和button3是一級的話,那button2,textView,button3既是二級)
只有同一級別的才能設(shè)置關(guān)系,否則的話設(shè)置相互之間的位置關(guān)系就不會起作用。

這就是筆者的理解,根本不存在層的概念。 

【編輯推薦】

  1. Windows Phone 7 免費線下培訓(xùn)火熱報名中
  2. 解析Android Widget開發(fā)中如何構(gòu)建Activity類
  3. Android Widget開發(fā)詳解
  4. Android Widget開發(fā)系列解讀
  5. Android Widget工具箱
責(zé)任編輯:佚名 來源: javaeye
相關(guān)推薦

2011-09-08 13:11:07

Android Wid實例

2010-07-13 09:02:19

Widget開發(fā)

2011-09-09 20:14:58

Android Wid

2011-09-08 15:51:33

Android Wid組件

2011-09-07 14:01:41

Android Wid實例

2011-09-07 17:54:40

Android Wid開發(fā)

2011-09-13 15:35:40

Widget

2011-09-07 10:58:07

Android wid

2011-09-07 14:20:42

Android Wid組件

2011-09-07 13:18:40

Android Wid

2011-09-07 13:42:36

Android Wid實例

2011-09-08 15:40:45

Android Wid組件

2011-09-07 10:34:48

Android Wid

2011-09-08 14:01:01

Android Wid實例

2011-09-08 13:36:26

Android Wid播放器

2011-09-08 15:29:50

Android Wid界面GridView

2011-09-09 13:23:17

Widget

2011-09-07 13:06:04

Android Wid

2011-09-07 16:28:46

QT WidgetQWidget

2011-09-08 13:41:53

Widget
點贊
收藏

51CTO技術(shù)棧公眾號