android 入门学习笔记 LayoutInflater的使用
来源:互联网 发布:最新免费聊天软件 编辑:程序博客网 时间:2024/05/18 00:50
在实际开发种LayoutInflater这个类还是非常有用的,它的作用类似于 findViewById(),
下面code为上例提到的MyAdapter 类的实现code
不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体 widget控件(如:Button,TextView等)。
用法有两种:
LayoutInflater inflater = LayoutInflater.from(this); View view=inflater.inflate(R.layout.ID, null); 或者干脆并成一句: View view=LayoutInflater.from(this).inflate(R.layout.ID, null);
另一种方法:
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); View view=inflater.inflate(R.layout.ID, null);
下面code为上例提到的MyAdapter 类的实现code
/* import相关class */import java.io.File;import java.util.List;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;/* 自定义的Adapter,继承android.widget.BaseAdapter */public class MyAdapter extends BaseAdapter{ /* 变量声明 mIcon1:回到根目录的图文件 mIcon2:回到上一层的图档 mIcon3:文件夹的图文件 mIcon4:文件的图档 */ private LayoutInflater mInflater; private Bitmap mIcon1; private Bitmap mIcon2; private Bitmap mIcon3; private Bitmap mIcon4; private List<String> items; private List<String> paths; /* MyAdapter的构造器,传入三个参数 */ public MyAdapter(Context context,List<String> it,List<String> pa) { /* 参数初始化 */ mInflater = LayoutInflater.from(context); items = it; paths = pa; mIcon1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.back01); mIcon2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.back02); mIcon3 = BitmapFactory.decodeResource(context.getResources(), R.drawable.folder); mIcon4 = BitmapFactory.decodeResource(context.getResources(), R.drawable.doc); } /* 因继承BaseAdapter,需覆盖以下方法 */ @Override public int getCount() { return items.size(); } @Override public Object getItem(int position) { return items.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position,View convertView,ViewGroup parent) { ViewHolder holder; if(convertView == null) { /* 使用自定义的file_row作为Layout */ convertView = mInflater.inflate(R.layout.file_row, null); /* 初始化holder的text与icon */ holder = new ViewHolder(); holder.text = (TextView) convertView.findViewById(R.id.text); holder.icon = (ImageView) convertView.findViewById(R.id.icon); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } File f=new File(paths.get(position).toString()); /* 设置[回到根目录]的文字与icon */ if(items.get(position).toString().equals("b1")) { holder.text.setText("Back to /"); holder.icon.setImageBitmap(mIcon1); } /* 设置[回到上一层]的文字与icon */ else if(items.get(position).toString().equals("b2")) { holder.text.setText("Back to .."); holder.icon.setImageBitmap(mIcon2); } /* 设置[文件或文件夹]的文字与icon */ else { holder.text.setText(f.getName()); if(f.isDirectory()) { holder.icon.setImageBitmap(mIcon3); } else { holder.icon.setImageBitmap(mIcon4); } } return convertView; } /* class ViewHolder */ private class ViewHolder { TextView text; ImageView icon; }}
- android 入门学习笔记 LayoutInflater的使用
- Android学习笔记 LayoutInflater
- Android学习之LayoutInflater类的使用
- android LayoutInflater的使用
- Android - LayoutInflater 的使用
- Android LayoutInflater的使用
- Android LayoutInflater的使用
- Android-LayoutInflater的使用
- Android LayoutInflater的使用
- Android LayoutInflater的使用 .
- Android LayoutInflater的使用
- Android LayoutInflater的使用
- Android:LayoutInflater的使用
- Android LayoutInflater的使用
- Android LayoutInflater的使用
- Android LayoutInflater的使用
- android LayoutInflater的使用
- Android LayoutInflater 的使用
- ios 工作日记--NSURL 中文 crash问题
- Visual Studio 2010中C++的四大变化
- silverlight获取ado.net数据源
- ttylinux tomcat问题
- 10个最酷的Linux单行命令
- android 入门学习笔记 LayoutInflater的使用
- HTML5时代真的来临了--我们拭目以待
- Java中abstract和interface的区别
- 用gdb调试core dump文件
- 一个简单的Hibernate例子
- Android编程14个很有用的代码片段
- android:如何在java代码中覆盖webview中的javascript事件
- .NET源码调试最常遇到问题解答
- JDK环境变量的配置