码迷,mamicode.com
首页 > 移动开发 > 详细

Android-RelativeLayout布局技巧(一)

时间:2015-02-01 13:21:50      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

 如果有一个需求是这样的,在标题中的右上角有一个button

技术分享

 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作为参照物

技术分享

 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>  

 

此篇主要是 相对参照物

Android-RelativeLayout布局技巧(一)

标签:

原文地址:http://www.cnblogs.com/royi123/p/4265412.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!