【转】Android中 layoyt.xml转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)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!--?xml version="1.0" encoding="utf-8"?--> 2 <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:core="http://schemas.android.com/apk/res/com.gxtag.gym" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="5dp"> 3 4 <imageview android:id="@+id/iv_head_album" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/defaulthead"> 5 6 </imageview></linearlayout>
2、main.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!--?xml version="1.0" encoding="utf-8"?--> 2 <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"> 3 4 <include android:layout_width="fill_parent" android:layout_height="wrap_content" layout="@layout/block_head_back"> 5 6 </include></linearlayout>
3、
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.gxtag.gym.ui; 2 3 import android.app.Activity; 4 import android.content.Context; 5 import android.os.Bundle; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.view.ViewGroup; 10 import android.widget.LinearLayout; 11 import android.widget.LinearLayout.LayoutParams; 12 import android.widget.TextView; 13 14 import com.gxtag.gym.R; 15 import com.icq.app.widget.StatedButton; 16 17 public class DynamicViewActivity extends Activity implements OnClickListener{ 18 19 private Context mContext; 20 private TextView mTv_title; 21 private String title = "动态添加布局"; 22 private StatedButton mSbtn_back; 23 private LinearLayout mLl_parent; 24 25 @Override 26 protected void onCreate(Bundle savedInstanceState) { 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.activity_dynamic); 29 mContext=this; 30 initView(); 31 mLl_parent.addView(addView1()); 32 mLl_parent.addView(addView2()); 33 34 } 35 36 private void initView() { 37 // TODO 初始化视图 38 mLl_parent=(LinearLayout) findViewById(R.id.ll_parent); 39 mTv_title = (TextView) findViewById(R.id.tv_title); 40 mTv_title.setText(String.format(String.format( 41 getResources().getString(R.string.title), title))); 42 mSbtn_back = (StatedButton) findViewById(R.id.sbtn_navback); 43 mSbtn_back.setOnClickListener(this); 44 45 46 } 47 48 private View addView1() { 49 // TODO 动态添加布局(xml方式) 50 LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( 51 LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 52 // LayoutInflater inflater1=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 53 // LayoutInflater inflater2 = getLayoutInflater(); 54 LayoutInflater inflater3 = LayoutInflater.from(mContext); 55 View view = inflater3.inflate(R.layout.block_gym_album_list_item, null); 56 view.setLayoutParams(lp); 57 return view; 58 59 } 60 61 private View addView2() { 62 // TODO 动态添加布局(java方式) 63 LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( 64 LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 65 LinearLayout view = new LinearLayout(this); 66 view.setLayoutParams(lp);//设置布局参数 67 view.setOrientation(LinearLayout.HORIZONTAL);// 设置子View的Linearlayout// 为垂直方向布局 68 //定义子View中两个元素的布局 69 ViewGroup.LayoutParams vlp = new ViewGroup.LayoutParams( 70 ViewGroup.LayoutParams.WRAP_CONTENT, 71 ViewGroup.LayoutParams.WRAP_CONTENT); 72 ViewGroup.LayoutParams vlp2 = new ViewGroup.LayoutParams( 73 ViewGroup.LayoutParams.WRAP_CONTENT, 74 ViewGroup.LayoutParams.WRAP_CONTENT); 75 76 TextView tv1 = new TextView(this); 77 TextView tv2 = new TextView(this); 78 tv1.setLayoutParams(vlp);//设置TextView的布局 79 tv2.setLayoutParams(vlp2); 80 tv1.setText("姓名:"); 81 tv2.setText("李四"); 82 tv2.setPadding(calculateDpToPx(50), 0, 0, 0);//设置边距 83 view.addView(tv1);//将TextView 添加到子View 中 84 view.addView(tv2);//将TextView 添加到子View 中 85 return view; 86 } 87 88 private int calculateDpToPx(int padding_in_dp){ 89 final float scale = getResources().getDisplayMetrics().density; 90 return (int) (padding_in_dp * scale + 0.5f); 91 } 92 93 94 @Override 95 public void onClick(View v) { 96 // TODO 控件单击事件 97 switch (v.getId()) { 98 case R.id.sbtn_navback: 99 this.finish(); 100 break; 101 default: 102 break; 103 } 104 } 105 106 }