Android-RelativeLayoutレイアウトテクニック(一)

15709 ワード

需要がある場合は、タイトルの右上隅にbuttonがあります.
Android-RelativeLayout布局技巧(一)
 1     <?xml version="1.0" encoding="utf-8"?>  

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

 3         android:layout_width="match_parent"  

 4         android:layout_height="match_parent"  

 5         android:orientation="vertical" >  

 6      <RelativeLayout   

 7          android:layout_width="match_parent"  

 8          android:layout_height="45dp"  

 9          android:background="@color/pink_light">  

10          <TextView   

11              android:layout_width="wrap_content"  

12              android:layout_height="wrap_content"  

13              android:text="  "  

14              android:layout_centerInParent="true"  

15              />  

16            

17          <Button   

18              android:layout_width="45dp"  

19              android:layout_height="26dp"  

20              android:layout_alignParentRight="true"  

21              android:layout_marginRight="10dp"  

22              android:layout_centerVertical="true"  

23              android:background="@color/green_light"  

24              android:text="  "  

25              android:textSize="12sp"  

26              />  

27      </RelativeLayout>     

28     </LinearLayout>  

実は上の例では、相対レイアウトの相対親コントロールを使用しています.中央、右、右からどのくらいdb離れていますか.もちろん親コントロールが多いので、自分で参照物を定義します.例えば、タイトルのTextViewを参照物として使用することができます.
Android-RelativeLayout布局技巧(一)
 1     <?xml version="1.0" encoding="utf-8"?>  

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

 3         android:layout_width="match_parent"  

 4         android:layout_height="match_parent"  

 5         android:orientation="vertical" >  

 6      <RelativeLayout   

 7          android:layout_width="match_parent"  

 8          android:layout_height="45dp"  

 9          android:background="@color/pink_light">  

10          <TextView   

11              android:id="@+id/title"  

12              android:layout_width="wrap_content"  

13              android:layout_height="wrap_content"  

14              android:text="  "  

15              android:layout_centerInParent="true"  

16              />  

17          <TextView   

18              android:layout_width="wrap_content"  

19              android:layout_height="wrap_content"  

20              android:text="  2"  

21              android:layout_toRightOf="@+id/title"  

22              android:layout_centerInParent="true"  

23              android:layout_marginLeft="5dp"  

24              />  

25      </RelativeLayout>     

26     </LinearLayout>  

中央参照
 1 <RelativeLayout

 2         android:layout_width="match_parent"

 3         android:background="@color/white"

 4         android:layout_gravity="center_horizontal"

 5         android:layout_height="wrap_content"

 6         android:layout_marginTop="10dp"

 7         >

 8         <Button

 9             android:id="@+id/common_dialog_sureBtn"

10             android:layout_width="@dimen/common_button_width"

11             android:layout_height="@dimen/common_button_height"

12             android:background="@drawable/common_click_bule_btn_selector"

13             android:text="@string/sure"

14             android:gravity="center"

15             android:textColor="@color/white"

16             android:textSize="@dimen/common_words_size"

17             android:layout_marginRight="30dp"

18             android:layout_toLeftOf="@+id/target_mid"/>

19           <View 

20             android:id="@+id/target_mid"

21             android:layout_width="0dp"

22             android:layout_height="0dp"

23             android:layout_centerHorizontal="true"

24             />

25         <Button

26             android:id="@+id/common_dialog_cancleBtn"

27             android:layout_width="@dimen/common_button_width"

28             android:layout_height="@dimen/common_button_height"

29             android:background="@drawable/common_click_gray_btn_selector"

30             android:text="@string/cencle"

31             android:gravity="center"

32             android:textColor="@color/white"

33             android:layout_toRightOf="@+id/target_mid"

34             android:layout_marginLeft="30dp"

35             android:textSize="@dimen/common_words_size"/>

36     </RelativeLayout>

この記事は主に相対参照物です