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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值