在alertdialog-Android中更改图像视图的可见性

时间:2014-02-07 06:07:15

标签: android android-layout android-imageview alertdialog android-alertdialog

我正在尝试构建一个自定义的alertdialog,其中有3个ImageViews。我想隐藏并动态显示这些ImageViews。这是我在HomeActivity类中的showroute(),其中我是m创建自定义对话框。

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,并且在检查某些条件后必须动态显示这些{{1}}。请帮助。

1 个答案:

答案 0 :(得分:1)

您正在再次给布局充气

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

现在初始化视图并设置视图的可见性。同时根据您的需要检查您是GONE还是INVISIBLE @ 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可见时捕捉

enter image description here