Android之RelativeLayout(相对布局)

RelativeLayout 相对布局

相对布局 RelativeLayout 允许子元素指定它们相对于其父元素或兄弟元素的位置,这是实际布局中最常用的布局方式之一。它灵活性大很多,当然属性也多,操作难度也大,属性之间产生冲突的的可能性也大,使用相对布局时要多做些测试。

<?xml version="1.0" encoding="utf-8"?>
<!-- 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>
 -->
 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"    
    android:layout_width="fill_parent"    
    android:layout_height="fill_parent">    
    <!-- system name label -->
    <TextView        
        android:id="@+id/label"        
        android:layout_width="fill_parent"        
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="#E77156"
        android:textSize="24dip"
	    android:background="#222"   
        android:text="@string/SystemName"
        />   
     <!-- User name -->
     <TextView        
        android:id="@+id/Username"        
        android:layout_width="fill_parent"        
        android:layout_height="wrap_content"    
        android:layout_marginTop = "10dp"  
        android:layout_below="@id/label"    
        android:text="@string/Username"
        />  
        
     <EditText        
         android:id="@+id/EditUsername"        
         android:layout_width="fill_parent"        
         android:layout_height="wrap_content"    
         android:background="@android:drawable/editbox_background"       
         android:layout_below="@id/Username"   
         android:inputType = "text"
         />   
    <!-- password -->    
	<TextView        
	      android:id="@+id/Password"        
	      android:layout_width="fill_parent"        
	      android:layout_height="wrap_content"    
	      android:layout_marginTop = "10dp"
	      android:layout_below="@id/EditUsername"      
	      android:text="@string/Password"
	      />  
	      
     <EditText        
          android:id="@+id/EditPssword"        
          android:layout_width="fill_parent"        
          android:layout_height="wrap_content"    
          android:background="@android:drawable/editbox_background"        
          android:layout_below="@id/Password"
          android:inputType = "text"
          />   
                
     <!-- button login -->
      <Button   
            android:id="@+id/Login"      
            android:layout_width="wrap_content"        
            android:layout_height="wrap_content"   
            android:layout_below="@id/EditPssword"     
            android:layout_marginTop = "10dp"   
            android:layout_alignParentRight="true"      
            android:text="@string/Login" 
            android:width="80dp"
            />
      <!-- button logout -->
      <Button        
            android:id="@+id/Logout"        
            android:layout_width="wrap_content"        
            android:layout_height="wrap_content"        
            android:layout_below="@id/EditPssword" 
            android:layout_marginTop = "10dp"  
            android:layout_toLeftOf="@id/Login"
            android:layout_marginRight="20dip"
            android:text="@string/Logout"
            android:width="80dp" 
            />   
            <!-- android:width设置同样的宽度 --> 
            <!-- android:layout_alignParentRight="true"  -->
            <!-- android:layout_marginLeft="10dip"     -->
</RelativeLayout>

效果图:


TextView控件居中,设置属性:
android:gravity="center"

下面介绍一下RelativeLayout用到的一些重要的属性:

第一类:属性值为true或false
android:layout_centerHrizontal                                           水平居中
android:layout_centerVertical                                            垂直居中
android:layout_centerInparent                                           相对于父元素完全居中
android:layout_alignParentBottom                                     贴紧父元素的下边缘
android:layout_alignParentLeft                                          贴紧父元素的左边缘
android:layout_alignParentRight                                        贴紧父元素的右边缘
android:layout_alignParentTop                                          贴紧父元素的上边缘
android:layout_alignWithParentIfMissing                            如果对应的兄弟元素找不到的话就以父元素做参照物

第二类:属性值必须为id的引用名“@id/id-name”
android:layout_below                          在某元素的下方
android:layout_above                          在某元素的的上方
android:layout_toLeftOf                       在某元素的左边
android:layout_toRightOf                     在某元素的右边

android:layout_alignTop                      本元素的上边缘和某元素的的上边缘对齐
android:layout_alignLeft                      本元素的左边缘和某元素的的左边缘对齐
android:layout_alignBottom                 本元素的下边缘和某元素的的下边缘对齐
android:layout_alignRight                    本元素的右边缘和某元素的的右边缘对齐

第三类:属性值为具体的像素值,如30dip,40px
android:layout_marginBottom              离某元素底边缘的距离
android:layout_marginLeft                   离某元素左边缘的距离
android:layout_marginRight                 离某元素右边缘的距离
android:layout_marginTop                   离某元素上边缘的距离



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值