在android中,LayoutInflater有點(diǎn)類似于Activity的findViewById(id),不同的是LayoutInflater是用來(lái)找layout下的xml布局文件,并且實(shí)例化!而findViewById()是找具體xml下的具體 widget控件(如:Button,TextView等)。
下面通過(guò)一個(gè)例子進(jìn)行詳細(xì)說(shuō)明:
1、在res/layout文件夾下,添加一個(gè)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,設(shè)置對(duì)話框內(nèi)容的View
dialog = builder.create();
dialog.show();
}
});
新聞熱點(diǎn)
疑難解答
圖片精選