RelativeLayout(相对布局)

简介: 相对布局的子控件会根据它们所设置的参照控件和参数进行相对布局。 参照控件:aclock   控件与容器之间 android:layout_alignParentLeft="true" 位于父容器左上角 android:layout_alignParentBottom, android:layou...

相对布局的子控件会根据它们所设置的参照控件和参数进行相对布局。

参照控件: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>

 

 

目录
相关文章
|
3月前
|
Android开发
08. 【Android教程】相对布局 RelativeLayout
08. 【Android教程】相对布局 RelativeLayout
36 0
|
3月前
|
Android开发
深入了解 Android 中的 FrameLayout 布局
深入了解 Android 中的 FrameLayout 布局
|
3月前
|
Android开发 开发者
深入了解 Android 中的 RelativeLayout 布局
深入了解 Android 中的 RelativeLayout 布局
|
容器
RelativeLayout(相对布局)
LinearLayout也是我们用的比较多的一个布局,我们更多的时候更钟情于他的weight(权重)属性,等比例划分,对屏幕适配还是帮助蛮大的;但是使用LinearLayout的时候也有一个问题,就是当界面比较复杂的时候,需要嵌套多层的LinearLayout,这样就会降低UI Render的效率(渲染速度),而且如果是listview或者GridView上的item,效率会更低,另外太多层LinearLayout嵌套会占用更多的系统资源,还有可能引发stackoverflow;但是如果我们使用RelativeLayout的话,可能仅仅需要一层就可以完成了,以父容器或者兄弟组件参考+margi
81 0
|
算法 Java Android开发
LinearLayout(线性布局)
本节开始讲Android中的布局,今天我们要讲解的就是第一个布局,LinearLayout(线性布局),我们屏幕适配的使用用的比较多的就是LinearLayout的weight(权重属性),在这一节里,我们会详细地解析LinearLayout,包括一些基本的属性,Weight属性的使用,以及比例如何计算,另外还会说下一个用的比较少的属性:android:divider绘制下划线!
63 0
|
XML Android开发 数据格式
Android开发中RelativeLayout相对布局
Android开发中RelativeLayout相对布局
314 0
Android开发中RelativeLayout相对布局
|
XML Android开发 数据格式