LayoutInflater详解
来源:互联网 发布:好听的网络翻唱 编辑:程序博客网 时间:2024/05/30 22:48
主布局main.xml里有一个TextView和一个Button,当点击Button,出现Dialog,而这个Dialog的布局方式是我们在layout目录下定义的custom_dialog.xml文件(里面左右分布,左边ImageView,右边TextView)。
XML代码:
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ShowCustomDialog"/>
</LinearLayout>
定义对话框的布局方式,我们在layout目录下,新建一个名为custom_dialog.xml文件具体代码如下:
XML代码:
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF"/>
</LinearLayout>
主程序LayouInflaterDemo.java代码如下:
packageEOE.android.Demo;
importandroid.app.Activity;
importandroid.app.AlertDialog;
importandroid.content.Context;
importandroid.os.Bundle;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.ImageView;
importandroid.widget.TextView;
publicclassLayoutInflaterDemoextendsActivityimplementsOnClickListener{
privateButtonbutton;
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
publicvoidonClick(Viewv){
showCustomDialog();
}
publicvoidshowCustomDialog(){
AlertDialog.Builderbuilder;
AlertDialogalertDialog;
ContextmContext=LayoutInflaterDemo.this;
//下面俩种方法都可以
////LayoutInflaterinflater=getLayoutInflater();
LayoutInflaterinflater=(LayoutInflater)mContext.getSystemServic(LAYOUT_INFLATER_SERVICE);
Viewlayout=inflater.inflate(R.layout.custom_dialog,null);
TextViewtext=(TextView)layout.findViewById(R.id.text);
text.setText("Hello,WelcometoMrWei'sblog!");
ImageViewimage=(ImageView)layout.findViewById(R.id.image);
image.setImageResource(R.drawable.icon);
builder=newAlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog=builder.create();
alertDialog.show();
}
}
- LayoutInflater 详解
- LayoutInflater详解
- LayoutInflater详解
- LayoutInflater详解
- LayoutInflater详解
- LayoutInflater详解
- LayoutInflater详解
- LayoutInflater详解
- layoutinflater详解
- LayoutInflater详解
- LayoutInflater 详解
- LayoutInflater详解
- layoutinflater详解
- LayoutInflater详解
- LayoutInflater 详解
- LayoutInflater详解
- Android LayoutInflater详解
- LayoutInflater inflate参数详解
- Linux下安装ossec
- HTTP协议详解
- in和exists的区别与SQL执行效率分析
- 做自己最重要
- 探索Android中的Parcel
- LayoutInflater详解
- 使用Ueditor
- seo推广
- Android 权限大全
- 什么是hibernate的延迟加载,什么时候使用延时加载,为什么要用延时加载?
- 关于内存对齐
- oracle 中数据文件丢失 导致登录不了 如何恢复?
- eclipse xfire插件安装
- js检测判断链接(URL)是否有效(服务可用)