不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体 widget控件.
package com.bivin;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
showCustomDialog();
}
public void showCustomDialog() {
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = MainActivity.this;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog, null);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, Welcome to Mr Wei's blog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.icon);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.show();
}
}
相关推荐
LayoutInflater的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化!
主要介绍了Android开发中LayoutInflater用法,结合实例形式分析了LayoutInflater类的功能、作用、使用方法及相关注意事项,需要的朋友可以参考下
Android Inflate()方法的作用是将xml定义的一个布局找出来,但仅仅是找出来而且隐藏的,没有找到的同时并显示功能。最近做的一个项目就是这一点让我迷茫了好几天。 Android上还有一个与Inflate()功能类似的方法叫...
在 实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。不同点是LayoutInflater是用 来找res/layout/下的xml布局文件,并且实例化;而findViewById()是找xml布局文件下的具体widget控件...
1.注意!实例化过的控件,例如Textview tx,如果再次...首先,获取需要加载的布局文件order.xml, 这里采用的是LayoutInflater,而不是我们平时使用的 findViewById( ). **LayoutInflater的作用类似于 findViewById
在详细实现之前,先介绍LayoutInflater,因为它在换肤中非常重要:它的作用是:将布局XML文件实例化为其对应的View对象。所以可以通过LayoutInflater获取到创建了多少View以及View对应的属性。 这里有鸿洋大佬的一篇...
运行效果:使用说明:继承自 RecyclerView.自定义 fling factor.自定义 paging trigger.支持 水平个垂直方向.支持 FragmentViewPager (api 12 )gradlecompile('com.lsjwzh:recyclerviewpager:1.0.2')xml: ...
00-本地时间及网络时间验证改版.js 00-正则匹配关闭应用-适用大部分手机(1).js 00-正则匹配关闭应用-适用大部分手机.js 00-简化点击控件.js 00-结束事件与结束应用(1).js 00-结束事件与结束应用.js 00-读&删...