2017-03-18 17 views
0

이미지가있는 AlertDialog를 표시하려고합니다. 그러나 상황에 따라 이미지가 달라질 수 있습니다. 프로그래밍 XML 내에서 이미지 뷰에 액세스 할 수있는 방법이 있나요Android - 변경 가능한 이미지가있는 경고 대화 상자

<?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> 

:

내가에 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입니다 파일 그래서 나는 이미지를 바꿀 수 있습니까? 아니면 더 좋은 방법이 있을까요?

답변

0

당신은 당신의 viewfindViewById를 호출하여 ImageView의 참조를 얻을 다음에 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(); 
+0

감사합니다! 그것은 효과가 있었다. –