在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();
}
});
新聞熱點
疑難解答
圖片精選