【问题标题】:Is it possible to have ListView inside a dialog?是否可以在对话框中使用 ListView?
【发布时间】:2011-10-03 04:24:11
【问题描述】:

基本上,我有这个最初由 [Fedor][1] 创建的 LazyList,我只是想知道是否有办法将它放在对话框中。请帮助我,我一直在努力解决这个问题,我真的需要你的帮助。提前致谢!

当你需要时,这里是他的代码:

    p

            list.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,long id) {  



                    if(position == 0){



                        final Dialog dialog = new Dialog(MainPictures.this, R.style.CustomDialogTheme);
                        dialog.setContentView(R.layout.customlayout);
                        dialog.setTitle(null);
                        dialog.setCancelable(true);
                        dialog.show();
                        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes(); 
                        lp.dimAmount=0.5f;  
                        dialog.getWindow().setAttributes(lp);  
                        dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);



                    }




                    else {
                        System.out.println("Error");
                    }

                    }                                                                                                       
                });


        }






        private String[] mStrings={
                "http://www.urlurl/hi.png",
                "http://www.urlurl/hi.png",

        };
    }

【问题讨论】:

  • 您希望“mStrings”出现在 listView..?
  • 是的,但就像我之前说的,它总是给我一个错误
  • 实际上,这整个代码就是我想要在我的对话框中出现的内容
  • @Lean 然后在您的活动标签“”下的 xml 中添加此标签
  • 看看我上面的代码,我把它简单化了一点。

标签: android listview dialog


【解决方案1】:

看好这个。

http://developer.android.com/guide/topics/ui/dialogs.html

更新:

另一种解决方案是创建一个活动并将 listView 放入其中并将其主题设置为对话框。

阅读本文了解如何设置主题http://developer.android.com/guide/topics/ui/themes.html

【讨论】:

  • 我知道如何创建基本对话框,但我不知道如何在对话框中创建列表视图
  • @Lean.. 你可以给Dialog添加一个列表,它看起来就像ListView,向下滚动并阅读整个文档。
  • 那里没有列表视图。请帮帮我
  • @Leon.. 没有 listView 但您可以将字符串列表添加到 AlertDialog,它看起来就像 AlertDialog。查看我的编辑以获取更新..
【解决方案2】:

您必须创建一个具有自定义布局的对话框。例如,使用这种布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/layout_root"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="400dip"
              android:padding="10dp">
    <ListView android:id="@+id/MyAwesomeList"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"/>
</LinearLayout>

当您在 Activity 的 onCreateDialog() 回调中创建对话框时,将其用于这样的对话框:

Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);

dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");

ListView myList = (ListView) dialog.findViewById(R.id.MyAwesomeList);
// set the list adapter and stuff

【讨论】:

  • 感谢您的帮助,但是当我使用 Fedor 的 ListView 时有点不同,它给了我这个错误“构造函数 LazyAdapterWallpaper(new AdapterView.OnItemClickListener(){}, String[]) is undefined”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-16
  • 1970-01-01
  • 2021-03-23
  • 1970-01-01
  • 1970-01-01
  • 2016-11-30
  • 1970-01-01
相关资源
最近更新 更多