RelativeLayout(相對布局)


相對布局的子控件會根據它們所設置的參照控件和參數進行相對布局。

參照控件:aclock   控件與容器之間

android:layout_alignParentLeft="true" 位於父容器左上角 android:layout_alignParentBottom, android:layout_alignParentTop, android:layout_alignParentRight  只能在父控件為RelativeLayout時才起作用,而對於像LinearLayout這樣的布局不起作用

android:layout_centerInParent="true" 位於布局容器的中央位置;

layout_centerHorizontal位於布局容器水平居中位置;

layout_centerVertical位於布局容器垂直居中位置

 

被參照控件:控件與控件之間位置

android:layout_below="@id/aclock" 位於aclock組件下方  

android:layout_toLeftOf="@id/dclock"位於dclock組件左則

控件與控件之間對齊方式  

android:layout_alignLeft="@id/aclock"與aclock組件左邊界對齊;

android:layout_alignTop="@id/aclock"與aclock組件上邊界對齊

 

效果:

代碼:

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     tools:context=".AndroidRelativeLayoutActivity" >
 6 
 7     <AnalogClock
 8         android:id="@+id/aclock"
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:layout_centerInParent="true" >
12     </AnalogClock>
13 
14         <!--
15 android:layout_below="@id/aclock" 位於模擬時鍾下面。如果沒有設置屬性layout_alignLeft和layout_marginLeft ,
16 該數字時鍾會頂到左屏幕邊顯示;alignLeft="@id/aclock" 和屬性layout_below 配合使用,使得該數字時鍾和上面的模擬時鍾的左邊距對齊,
17 如果沒有設置marginLeft 屬性的話和上面的兩個屬性配合使用,使得數字時鍾距模擬時鍾的左邊距40個像素
18 
19 -->
20     
21     <DigitalClock
22         android:id="@+id/dclock"
23         android:layout_width="wrap_content"
24         android:layout_height="wrap_content"
25         android:layout_alignLeft="@id/aclock"
26         android:layout_below="@id/aclock"
27         android:layout_marginLeft="40px" >
28     </DigitalClock>
29 
30     <TextView
31         android:layout_width="wrap_content"
32         android:layout_height="wrap_content"
33         android:layout_alignTop="@id/aclock"
34         android:layout_toLeftOf="@id/dclock"
35         android:text="當前時間" >
36     </TextView>
37 
38 </RelativeLayout>

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM