我想显示带图像的AlertDialog。但是图像可能会根据某些情况而改变。
这是我用于AlertDialog的代码:
AlertDialog.Builder alertadd = new AlertDialog.Builder(wheel.this);
LayoutInflater factory = LayoutInflater.from(wheel.this);
final View view = factory.inflate(R.layout.alert, null);
alertadd.setView(view);
alertadd.setTitle("Alert Dialog Title");
alertadd.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
}
});
alertadd.show();
这是alert.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/dialog_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image"
android:paddingLeft="5dp"
android:paddingRight="5dp"/>
</LinearLayout>
有没有办法以编程方式访问XML文件中的imageView,以便我可以更改图像?或者有更好的方法吗?
答案 0 :(得分:0)
您可以在ImageView
上致电findViewById
,然后在其上致电view
,获取setImageResource
的引用:
LayoutInflater factory = LayoutInflater.from(wheel.this);
final View view = factory.inflate(R.layout.alert, null);
// change the ImageView image source
final ImageView dialogImageView = (ImageView) view.findViewById(R.id.dialog_imageview);
dialogImageView.setImageResource(R.drawable.your_image);
alertadd.setView(view);
alertadd.setTitle("Alert Dialog Title");
alertadd.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
}
});
alertadd.show();