1

我試圖建立一個自定義alertdialog,其中有3 ImageViews。我想隱藏和動態顯示這些ImageViews。這裏是我的showroute()在HomeActivity類中,創建自定義對話框。更改alertdialog中的圖像瀏覽的可見性-Android

AlertDialog.Builder alertDialog = new AlertDialog.Builder(HomeActivity.this); 
    alertDialog.setView(getLayoutInflater().inflate(R.layout.routedialog, null)); 
    alertDialog.setTitle("Home Activity"); 
    alertDialog.setIcon(R.drawable.logo); 
    alertDialog.setMessage(Html.fromHtml("<b>Route Details</b>")); 
    ImageView iv1,iv2,iv3; 
    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 
    View convertView = (LinearLayout) inflater.inflate(R.layout.routedialog, null); 
    iv1=(ImageView) convertView.findViewById(R.id.iv1); 
    iv2=(ImageView) convertView.findViewById(R.id.iv2); 
    iv3=(ImageView) convertView.findViewById(R.id.iv3); 


    iv1.setVisibility(View.GONE); 
    iv2.setVisibility(View.GONE); 
    iv3.setVisibility(View.GONE); 

    convertView.findViewById(R.id.ll).invalidate(); 

,這裏是我的routedialog.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/ll" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:padding="10dp" > 
<ImageView 
      android:id="@+id/iv1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:src="@drawable/ivred" /> 
    <ImageView 
      android:id="@+id/iv2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:src="@drawable/ivblue" /> 
    <ImageView 
      android:id="@+id/iv3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:src="@drawable/ivgreen" /> 
</LinearLayout> 

我想這些ImageViews當對話管理器彈出,並具有一定的檢查幫助conditions.Please後動態地讓用戶看到它們被隱藏。

回答

1

您正在膨脹的佈局再次

LayoutInflater inflater = getLayoutInflater(); 
View convertView = (LinearLayout) inflater.inflate(R.layout.routedialog, null); 
alertDialog.setView(convertView); 

現在初始化意見並設置視圖的可視性。還請檢查您是否想要GONEINVISIBLE @http://developer.android.com/reference/android/view/View.html#attr_android:visibility取決於您的需要

另外,我在您的自定義對話框佈局中看不到任何地方iv4,iv5,iv6,iv7,iv8

iv4=(ImageView) convertView.findViewById(R.id.iv4); 
    iv5=(ImageView) convertView.findViewById(R.id.iv5); 
    iv6=(ImageView) convertView.findViewById(R.id.iv6); 
    iv7=(ImageView) convertView.findViewById(R.id.iv7); 
    iv8=(ImageView) convertView.findViewById(R.id.iv8); 

會給NullPointerException

編輯:

AlertDialog.Builder alertDialog = new AlertDialog.Builder(HomeActivity.this); 
     LayoutInflater inflater = getLayoutInflater(); 
     View convertView = (View) inflater.inflate(R.layout.routedialog, null); 
     alertDialog.setView(convertView); 
     alertDialog.setTitle("Home Activity"); 
     alertDialog.setIcon(R.drawable.ic_launcher); 
     alertDialog.setMessage(Html.fromHtml("<b>Route Details</b>")); 
     ImageView iv1,iv2,iv3; 
     iv1=(ImageView) convertView.findViewById(R.id.iv1); 
     iv2=(ImageView) convertView.findViewById(R.id.iv2); 
     iv3=(ImageView) convertView.findViewById(R.id.iv3); 
     iv1.setVisibility(View.GONE); // GONE oR INVISIBLE according to what you want 
     iv2.setVisibility(View.GONE); 
     iv3.setVisibility(View.GONE); 
     alertDialog.show(); 

捕捉時的ImageView可見烏拉圭回合answer..But我希望在對話窗口彈出,應該是圖像的觀點是無形的

enter image description here

+0

謝謝在檢查一些條件後顯示。 – Lal

+0

@Lal將可見性設置爲'INVISIBLE'。或者在xml中爲視圖'andorid:visibility =「INVISIBLE」'。然後在條件使其可見 – Raghunandan

+0

@Lal的快照是隻顯示你的對話框作品 – Raghunandan