首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

android运用代码进行布局

2012-06-27 
android使用代码进行布局大家都知道,在使用eclipse进行android开发的时候,我们可以通过布局文件(.xml文件)

android使用代码进行布局
大家都知道,在使用eclipse进行android开发的时候,我们可以通过布局文件(.xml文件)对程序的界面进行布局。同时肯定能想到,通过代码也可以实现同样的功能。只是平时会用得少,且比较麻烦,但在某些情况下还是挺有用的。下面就来说明如何通过代码进行:

我依然用到了两个.xml文件,left.xml、right.xml, 内容如下left.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/left"     android:layout_width="fill_parent"     android:layout_height="fill_parent" >     
<TextView        
android:id="@+id/view1"        
android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:background="@drawable/blue"        
android:text="@string/left_view1" /> 
   
<TextView        
android:id="@+id/view2"        
android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:layout_below="@id/view1"        
android:background="@drawable/yellow"        
android:text="@string/left_view2" /> 
</RelativeLayout>

right.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/right"     android:layout_width="fill_parent"     android:layout_height="fill_parent" >     
<TextView        
android:id="@+id/right_view1"        
android:layout_width="fill_parent"        
android:layout_height="wrap_content"        
android:background="@drawable/yellow"        
android:gravity="center"        
android:text="@string/right_view1" />     

<TextView        
android:id="@+id/right_view2"        
android:layout_width="fill_parent"        
android:layout_height="wrap_content"         android:layout_below="@id/right_view1"        
android:background="@drawable/blue"        
android:gravity="center"        
android:text="@string/right_view2" /> 
</RelativeLayout>
源文件代码实现如下:

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.RelativeLayout; 
public class ActivityLayout extends Activity {    
@Override    
protected void onCreate(Bundle savedInstanceState) {        
// TODO Auto-generated method stub        
super.onCreate(savedInstanceState);        
// 通过代码创建一个linearlayout并将它设为activity的内容        
LinearLayout layoutmain = new LinearLayout(this);         layoutmain.setOrientation(LinearLayout.HORIZONTAL);         setContentView(layoutmain);         
// 获得具备XML解析功能的LayoutInflater        
LayoutInflater inflate = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);                 
//解析left.xml文件构造RelativeLayout        
RelativeLayout layoutLeft = (RelativeLayout) inflate.inflate(                 R.layout.left, null);                 
//解析right.xml文件构造RelativeLayout        
RelativeLayout layoutRight = (RelativeLayout) inflate.inflate(                 R.layout.right, null);         
//设置相关的控件在layoutmain中的摆放参数        
RelativeLayout.LayoutParams leftParam = new RelativeLayout.LayoutParams(                 RelativeLayout.LayoutParams.WRAP_CONTENT,                 RelativeLayout.LayoutParams.WRAP_CONTENT);         
RelativeLayout.LayoutParams rightParam = new RelativeLayout.LayoutParams(                 RelativeLayout.LayoutParams.FILL_PARENT,                 RelativeLayout.LayoutParams.WRAP_CONTENT);         
//将空间添加到layoutmain中        
layoutmain.addView(layoutLeft, leftParam);        
layoutmain.addView(layoutRight, rightParam);     }


本文出自 “lilingshui” 博客,请务必保留此出处http://qsjming.blog.51cto.com/1159640/748664

热点排行