您的位置:首页 > 移动开发 > Android开发

android使用代码进行布局

2011-12-22 16:25 465 查看
大家都知道,在使用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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: