基于Android在布局中动态添加视图的两种方法(总结)

  

  

添加视图文件的时候有两种方式:1,通过在xml文件定义布局;2、java代码编写

  

  

  

  

提到addview,首先要了解一下LayoutInflater类。这个类最主要的功能就是实现将xml表述的布局转化为视图的功能,为了便于理解,我们可以将它与findViewById()作一比较,二者都是实例化某一对象,不同的是findViewById()是找xml布局文件下的具体部件控件实例化,而LayoutInflater找res/布局下的xml布局文件来实例化的。

  

<强>(1)创建

  

LayoutInflater增压泵=(LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);或

  

LayoutInflater增压泵=LayoutInflater.from(活性);或

  

LayoutInflater增压泵=getLayoutInflater ();

  

这三种方法本质是相同的。

  

<强>(2)膨胀()

  

用LayoutInflater.inflate()将布局文件转化成视图。

  

视图查看=inflater.inflate(出来。

block_gym_album_list_item, null);   

  

  

<强> 1,通过在xml文件定义布局(block_gym_album_list_item.xml)
  

        linearlayout & lt;   xmlns: android=" http://schemas.android.com/apk/res/android "   android: layout_width="宽和"   android: layout_height="宽和"   android:取向=按怪薄?   android:填充=" 5 dp "比;      & lt; imageview   android: id=癅 + id/iv_head_album”   android: layout_width=" wrap_content "   android: layout_height=" wrap_content "   android: src=" https://www.yisu.com/zixun/@drawable/defaulthead”比;   & lt;/imageview>   & lt;/linearlayout>      

activity_dynamic         & lt; linearlayout xmlns: android=" http://schemas.android.com/apk/res/android "   android: id=癅 + id/ll_parent”   android: layout_width="宽和"   android: layout_height=" wrap_content "   面向android:=按怪薄北?      & lt;包括   android: layout_width="宽和"   android: layout_height=" wrap_content "   布局=癅layout/block_head_back”比;   & lt;/include>   & lt;/linearlayout>      之前      

<强> 3,MainActivity

        包com.gxtag.gym.ui;      进口android.app.Activity;   进口android.content.Context;   进口android.os.Bundle;   进口android.view.LayoutInflater;   进口android.view.View;   进口android.view.View.OnClickListener;   进口android.view.ViewGroup;   进口android.widget.LinearLayout;   进口android.widget.LinearLayout.LayoutParams;   进口android.widget.TextView;      进口com.gxtag.gym.R;   进口com.icq.app.widget.StatedButton;      公共类MainActivityextends活动实现OnClickListener {      私人上下文mContext;   私人TextView mTv_title;   私人字符串title=岸砑硬季帧?   私人StatedButton mSbtn_back;   私人LinearLayout mLl_parent;      @Override   保护无效onCreate(包savedInstanceState) {   super.onCreate (savedInstanceState);   setContentView (R.layout.activity_dynamic);   mContext=;   initView ();   mLl_parent.addView (addView1 ());   mLl_parent.addView (addView2 ());   }      私人空间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 (   getresource () .getString (R.string.title),标题)));   mSbtn_back=(StatedButton) findViewById (R.id.sbtn_navback);   mSbtn_back.setOnClickListener(这个);   }      预展addView1 () {//TODO动态添加布局(xml方式)   LinearLayout。LayoutParams lp=new LinearLayout.LayoutParams (   LayoutParams。宽和LayoutParams.WRAP_CONTENT);//LayoutInflater inflater1=(LayoutInflater) mContext.getSystemService (Context.LAYOUT_INFLATER_SERVICE);//LayoutInflater inflater2=getLayoutInflater ();   LayoutInflater inflater3=LayoutInflater.from (mContext);   视图查看=inflater3.inflate(出来。block_gym_album_list_item, null);   view.setLayoutParams (lp);   返回视图;   }      预展addView2 () {//TODO动态添加布局(java方式)   LinearLayout。LayoutParams lp=new LinearLayout.LayoutParams (   LayoutParams。宽和LayoutParams.WRAP_CONTENT);   LinearLayout视图LinearLayout=new ();   view.setLayoutParams (lp);//设置布局参数   view.setOrientation (LinearLayout.HORIZONTAL);//设置子视图的Linearlayout//为垂直方向布的局//定义子视图中两个元素的布局   ViewGroup。=new ViewGroup.LayoutParams (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(这个);   TextView tv2=new TextView(这个);   tv1.setLayoutParams(车牌区域);//设置TextView的布局   tv2.setLayoutParams (vlp2);   tv1.setText(“姓名:”);   tv2.setText(“李四”);   tv2.setPadding (calculateDpToPx(50), 0, 0, 0);//设置边距   view.addView (tv1);//将TextView添加到子视图中   view.addView(天空);//将TextView添加到子视图中   返回视图;   }      私人int calculateDpToPx (int padding_in_dp) {   最后的浮动比例=getresource () .getDisplayMetrics () .density;   返回(int) (padding_in_dp *规模+ 0.5 f);   }         @Override   公共空间onClick(查看v) {//TODO控件单击事件   开关(v.getId ()) {   案例R.id.sbtn_navback:   this.finish ();   打破;   默认值:   打破;   }   }      }

基于Android在布局中动态添加视图的两种方法(总结)