android动态加载布局文件示例
程序员文章站
2022-06-10 19:13:26
一、布局文件part.xml:
复制代码 代码如下:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingbottom="@dimen/activity_vertical_margin"
android:paddingleft="@dimen/activity_horizontal_margin"
android:paddingright="@dimen/activity_horizontal_margin"
android:paddingtop="@dimen/activity_vertical_margin"
tools:context=".mainactivity" >
<button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margintop="64dp"
android:text="添加" />
<edittext
android:id="@+id/edittext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" >
<requestfocus />
</edittext>
</relativelayout>
package com.example.codeui;
import android.os.bundle;
import android.app.activity;
import android.content.context;
import android.view.layoutinflater;
import android.view.menu;
import android.view.view;
import android.view.viewgroup;
import android.widget.linearlayout;
import android.widget.textview;
public class mainactivity extends activity {
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
linearlayout layout=new linearlayout(this);
layout.setorientation(linearlayout.vertical);//显示方向
//将view对象添加到布局界面
textview textview =new textview(this);
textview.settext("hello code ui");
viewgroup.layoutparams params = new viewgroup.layoutparams(
viewgroup.layoutparams.fill_parent,viewgroup.layoutparams.wrap_content);
//添加
layout.addview(textview,params);
//添加外部xml定义的布局
view view = getpartview();
layout.addview(view);
viewgroup.layoutparams layoutparams = new viewgroup.layoutparams(
viewgroup.layoutparams.fill_parent,viewgroup.layoutparams.fill_parent);
setcontentview(layout, layoutparams);
//setcontentview(r.layout.activity_main);
//采用代码编写效率高,但是很难维护
}
//通过加载xml文件将view添加到布局中
public view getpartview() {
//将xml布局文件生成view对象通过layoutinflater
layoutinflater inflater =(layoutinflater)getsystemservice(context.layout_inflater_service);
//将view对象挂载到那个父元素上,这里没有就为null
return inflater.inflate(r.layout.part, null);
}
@override
public boolean oncreateoptionsmenu(menu menu) {
getmenuinflater().inflate(r.menu.main, menu);
return true;
}
}
一、布局文件part.xml:
复制代码 代码如下:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingbottom="@dimen/activity_vertical_margin"
android:paddingleft="@dimen/activity_horizontal_margin"
android:paddingright="@dimen/activity_horizontal_margin"
android:paddingtop="@dimen/activity_vertical_margin"
tools:context=".mainactivity" >
<button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margintop="64dp"
android:text="添加" />
<edittext
android:id="@+id/edittext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" >
<requestfocus />
</edittext>
</relativelayout>
二、通过后台代码生成前台布局:
复制代码 代码如下:
package com.example.codeui;
import android.os.bundle;
import android.app.activity;
import android.content.context;
import android.view.layoutinflater;
import android.view.menu;
import android.view.view;
import android.view.viewgroup;
import android.widget.linearlayout;
import android.widget.textview;
public class mainactivity extends activity {
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
linearlayout layout=new linearlayout(this);
layout.setorientation(linearlayout.vertical);//显示方向
//将view对象添加到布局界面
textview textview =new textview(this);
textview.settext("hello code ui");
viewgroup.layoutparams params = new viewgroup.layoutparams(
viewgroup.layoutparams.fill_parent,viewgroup.layoutparams.wrap_content);
//添加
layout.addview(textview,params);
//添加外部xml定义的布局
view view = getpartview();
layout.addview(view);
viewgroup.layoutparams layoutparams = new viewgroup.layoutparams(
viewgroup.layoutparams.fill_parent,viewgroup.layoutparams.fill_parent);
setcontentview(layout, layoutparams);
//setcontentview(r.layout.activity_main);
//采用代码编写效率高,但是很难维护
}
//通过加载xml文件将view添加到布局中
public view getpartview() {
//将xml布局文件生成view对象通过layoutinflater
layoutinflater inflater =(layoutinflater)getsystemservice(context.layout_inflater_service);
//将view对象挂载到那个父元素上,这里没有就为null
return inflater.inflate(r.layout.part, null);
}
@override
public boolean oncreateoptionsmenu(menu menu) {
getmenuinflater().inflate(r.menu.main, menu);
return true;
}
}
上一篇: android自定义toast(widget开发)示例
下一篇: android应用签名详细步骤