续上,RelativeLayout:相对布局,相对布局顾名思义就是每个组件的排列都要有个组件作为参考,相对与你参考的组件放的位置。和之前的一样,布局方式可以通过两种方式来设置我们的UI布局。

先看只设置xml文件的效果图:

 

 

android之Layout(四)

 xml文件:

 


  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     xmlns:tools="http://schemas.android.com/tools" 
  3.     android:id="@+id/relative" //设置RelativeLayout布局的id,待会在Actvaty.java文件中将会用到
  4.     android:layout_width="match_parent" 
  5.     android:layout_height="match_parent" 
  6.     tools:context=".RelativeActivity" > 
  7.  
  8.     <ImageView   
  9.         android:id="@+id/cocos2d" 
  10.         android:src="@drawable/cocos2d" //设置图片组件的资源图片
  11.         android:layout_width="90dp"  //设置组件的宽度
  12.         android:layout_height="90dp"  //设置组件的高度
  13.          
  14.         /> 
  15.     <ImageView   
  16.         android:id="@+id/select" 
  17.         android:src="@drawable/closeselected" 
  18.         android:layout_width="wrap_content" 
  19.         android:layout_height="wrap_content" 
  20.         android:layout_toRightOf="@+id/cocos2d"  //该图片组件放在cocos2d图片组件的右边
  21.         android:contentDescription="p_w_picpath2" 
  22.         /> 
  23.     <TextView 
  24.         android:id="@+id/mytext" 
  25.         android:layout_width="wrap_content" 
  26.         android:layout_height="wrap_content" 
  27.         android:layout_below="@+id/select" 
  28.         android:layout_toRightOf="@+id/cocos2d" //该件放在cocos2d图片组件的右边
  29.         android:text="@string/hello_world" />  //设置组件的内容
  30.  
  31. </RelativeLayout> 

下面是在之前的基础之上,通过Activaty.java文件添加一个EditText组件

先看效果图片:

 

android之Layout(四) 

上面我们是将我们的EditText组件设置在cocos2d图片组件的右边,TextView组件的下边

下面看看Activaty.java的文件

 


  1. package com.cheng.relativelayout;  
  2.  
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.view.ViewGroup;  
  7. import android.widget.EditText;  
  8. import android.widget.RelativeLayout;  
  9. import android.widget.RelativeLayout.LayoutParams;  
  10.  
  11. public class RelativeActivity extends Activity {  
  12.  
  13.     @Override 
  14.     protected void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         super.setContentView(R.layout.main);  
  17.         //通过id获得在main中定义的RelativeLayout布局  
  18.         RelativeLayout mRelativeLayout = (RelativeLayout)findViewById(R.id.relative);  
  19.         //定义一个RelativeLayout布局的参数  
  20.         RelativeLayout.LayoutParams reParams = new RelativeLayout.LayoutParams(  
  21.                 ViewGroup.LayoutParams.FILL_PARENT, //填充满剩下的空间      
  22.                 ViewGroup.LayoutParams.FILL_PARENT);  
  23.         //将我们的布局参数这在mytext的下边  
  24.         reParams.addRule(RelativeLayout.BELOW, R.id.mytext);  
  25.         //将我们的布局参数设置在cocos2d这张图片的右边  
  26.         reParams.addRule(RelativeLayout.RIGHT_OF, R.id.cocos2d);  
  27.         //定义一个EditText组件  
  28.         EditText mEditText = new EditText(this);  
  29.         //将组件添加到我们的布局之中  
  30.         mRelativeLayout.addView(mEditText, reParams);  
  31.           
  32.     }  
  33.  
  34.     @Override 
  35.     public boolean onCreateOptionsMenu(Menu menu) {  
  36.         // Inflate the menu; this adds items to the action bar if it is present.  
  37.         getMenuInflater().inflate(R.menu.main, menu);  
  38.         return true;  
  39.     }  
  40.  
  41. }  

 

 

ok!