在android中,layoutinflater有点类似于activity的findviewbyid(id),不同的是layoutinflater是用来找layout下的xml布局文件,并且实例化!而findviewbyid()是找具体xml下的具体 widget控件(如:button,textview等)。
下面通过一个例子进行详细说明:
1、在res/layout文件夹下,添加一个xml文件dialog.xml
<imageview
android:id="@+id/diaimage"
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
</imageview>
<textview
android:id="@+id/diatv"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
</linearlayout>
textview diatv = (textview) layout.findviewbyid(r.id.diatv);
diatv.settext("welcome to layoutinflater study");
imageview image = (imageview) layout.findviewbyid(r.id.diaimage);
image.setimageresource(r.drawable.ic_launcher);
builder.setview(layout);// <--important,设置对话框内容的view
dialog = builder.create();
dialog.show();
}
});
如对本文有疑问, 点击进行留言回复!!
android RecyclerView checkbox复用解决
Android Q Input (1) -- InputManagerService启动
android -- ndk (stack corruption detected)
Android Span富文本图文混排 - ImageSpan(图文垂直居中)
网友评论