基于Android在布局中动态添加view的两种方法(总结)
一、说明
添加视图文件的时候有两种方式:1、通过在xml文件定义layout;2、java代码编写
二、前言说明
1.构造xml文件
2.layoutinflater
提到addview,首先要了解一下layoutinflater类。这个类最主要的功能就是实现将xml表述的layout转化为view的功能。为了便于理解,我们可以将它与findviewbyid()作一比较,二者都是实例化某一对象,不同的是findviewbyid()是找xml布局文件下的具体widget控件实例化,而layoutinflater找res/layout/下的xml布局文件来实例化的。
(1)创建
layoutinflater inflater=(layoutinflater)context.getsystemservice(context.layout_inflater_service);或
layoutinflater inflater = layoutinflater.from(activity.this);或
layoutinflater inflater = getlayoutinflater();
这三种方法本质是相同的。
(2)inflate()
用layoutinflater.inflate() 将layout文件转化成view。
view view = inflater.inflate(r.layout.block_gym_album_list_item, null);
3.添加视图文件
三、步骤
1、通过在xml文件定义layout(block_gym_album_list_item.xml)
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="5dp"> <imageview android:id="@+id/iv_head_album" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/defaulthead"> </imageview> </linearlayout>
activity_dynamic
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ll_parent" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <include android:layout_width="fill_parent" android:layout_height="wrap_content" layout="@layout/block_head_back"> </include> </linearlayout>
3、mainactivity
package com.gxtag.gym.ui; import android.app.activity; import android.content.context; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.widget.linearlayout; import android.widget.linearlayout.layoutparams; import android.widget.textview; import com.gxtag.gym.r; import com.icq.app.widget.statedbutton; public class mainactivityextends activity implements onclicklistener{ private context mcontext; private textview mtv_title; private string title = "动态添加布局"; private statedbutton msbtn_back; private linearlayout mll_parent; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_dynamic); mcontext=this; initview(); mll_parent.addview(addview1()); mll_parent.addview(addview2()); } private void initview() { // todo 初始化视图 mll_parent=(linearlayout) findviewbyid(r.id.ll_parent); mtv_title = (textview) findviewbyid(r.id.tv_title); mtv_title.settext(string.format(string.format( getresources().getstring(r.string.title), title))); msbtn_back = (statedbutton) findviewbyid(r.id.sbtn_navback); msbtn_back.setonclicklistener(this); } private view addview1() { // todo 动态添加布局(xml方式) linearlayout.layoutparams lp = new linearlayout.layoutparams( layoutparams.fill_parent, layoutparams.wrap_content); //layoutinflater inflater1=(layoutinflater)mcontext.getsystemservice(context.layout_inflater_service); // layoutinflater inflater2 = getlayoutinflater(); layoutinflater inflater3 = layoutinflater.from(mcontext); view view = inflater3.inflate(r.layout.block_gym_album_list_item, null); view.setlayoutparams(lp); return view; } private view addview2() { // todo 动态添加布局(java方式) linearlayout.layoutparams lp = new linearlayout.layoutparams( layoutparams.fill_parent, layoutparams.wrap_content); linearlayout view = new linearlayout(this); view.setlayoutparams(lp);//设置布局参数 view.setorientation(linearlayout.horizontal);// 设置子view的linearlayout// 为垂直方向布局 //定义子view中两个元素的布局 viewgroup.layoutparams vlp = new viewgroup.layoutparams( viewgroup.layoutparams.wrap_content, viewgroup.layoutparams.wrap_content); viewgroup.layoutparams vlp2 = new viewgroup.layoutparams( viewgroup.layoutparams.wrap_content, viewgroup.layoutparams.wrap_content); textview tv1 = new textview(this); textview tv2 = new textview(this); tv1.setlayoutparams(vlp);//设置textview的布局 tv2.setlayoutparams(vlp2); tv1.settext("姓名:"); tv2.settext("李四"); tv2.setpadding(calculatedptopx(50), 0, 0, 0);//设置边距 view.addview(tv1);//将textview 添加到子view 中 view.addview(tv2);//将textview 添加到子view 中 return view; } private int calculatedptopx(int padding_in_dp){ final float scale = getresources().getdisplaymetrics().density; return (int) (padding_in_dp * scale + 0.5f); } @override public void onclick(view v) { // todo 控件单击事件 switch (v.getid()) { case r.id.sbtn_navback: this.finish(); break; default: break; } } }
以上这篇基于android在布局中动态添加view的两种方法(总结)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。