android中LayoutInflater的使用(2)
Java代码
//基本用法
public void showCustomDialog(){
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = AppActivity.this;
//下面俩种方法都可以
//LayoutInflater inflater = getLayoutInflater();
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();
}
}
protected void showToast(int type) {
Toast.makeText(this, "*********", Toast.LENGTH_LONG).show();
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = li.inflate(R.layout.toast, null);
Toast toast = new Toast(this);
toast.setView(view);
toast.setDuration(type);
toast.show();
}
作者“Android学习心得”
相关新闻>>
- 发表评论
-
- 最新评论 更多>>