LayoutInflate的使用
来源:互联网 发布:旅游业发达的国家知乎 编辑:程序博客网 时间:2024/04/30 01:52
LayoutInflater作用是将layout的xml布局文件实例化为View类对象。
LayoutInflater在Android中是“扩展”的意思,作用类似于findViewById(),不同的是LayoutInflater是用来获得布局文件对象的,而findViewById()是用来获得具体控件的。LayoutInflater经常在BaseAdapter的getView方法中用到,用来获取整个View并返回。
获取LayoutInflater有三种方法:
LayoutInflater inflate = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);LayoutInflater inflate = getLayoutInflater();//在Activity中使用LayoutInflater inflate = LayoutInflater.from(getApplicationContext());
用法举例:
user_folder.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" > <requestFocus /> </EditText> <Button android:id="@+id/button1" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /></LinearLayout>
MainActivity.java:
package com.example.hello;import android.os.Bundle;import android.os.Handler;import android.app.Activity;import android.view.LayoutInflater;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.RelativeLayout;import android.widget.Toast;public class MainActivity extends Activity implements View.OnClickListener{private RelativeLayout frame;private View mySelfView1 = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); frame = (RelativeLayout) findViewById(R.id.frame); Button btn = (Button) findViewById(R.id.button1); btn.setOnClickListener(this); // 第一种方式 // LayoutInflater inflate = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); // 第二种方式 // LayoutInflater inflate = getLayoutInflater(); LayoutInflater inflate = LayoutInflater.from(getApplicationContext()); mySelfView1 = inflate.inflate(R.layout.user_folder, null); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }@Overridepublic void onClick(View v) {// TODO Auto-generated method stub if(mySelfView1 != null){ Toast.makeText(getApplicationContext(), "show my selfview", Toast.LENGTH_SHORT).show(); frame.addView(mySelfView1); Handler handler = new Handler(); handler.postDelayed(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubframe.removeView(mySelfView1);}}, 2000); } else { Toast.makeText(getApplicationContext(), "selfview is null", Toast.LENGTH_SHORT).show(); }} }
- LayoutInflate的使用
- LayoutInflate的使用
- layoutinflate.inflate方法的使用
- LayoutInflate(布局填充器)的使用
- 关于LayoutInflate的用法
- 关于LayoutInflate的理解。
- Android LayoutInflate的基本知识
- Layoutinflate的inflate用法
- LayoutInflate
- LayoutInflate
- 关于LayoutInflate的参数关系
- 使用LayoutInflate(视图扩充器)
- 关于LayoutInflate.inflate()方法的测试
- android LayoutInflate.inflate源码分析及使用区分
- Android LayoutInflate深度解析 给你带来全新的认识
- Android LayoutInflate
- layoutInflate 解析
- layoutinflate应用
- 微软面试100道之 5 查找最小的k个元素(数组)
- Javase 2.java技术分类及版本,java虚拟机
- 我的第一天博客
- 【Kinect for Windows SDK MSDN文档翻译】之二
- ___THE FIRST
- LayoutInflate的使用
- Android 二维码 生成和识别(附Demo源码)
- StringBuffer和StringBuilder的区别
- oracle概述
- ExtJS4.2学习(7)——基础知识之Reader&Writer篇
- HTTP Header 详解
- 已知一个数出现的次数超过了一半,请用O(n)的复杂度的算法找出这个数。
- Spring动态加载配置文件
- 工作队列(workqueue) create_workqueue/schedule_work/queue_work