【问题标题】:How can I create custom alert dialog with grid view in android?如何在 android 中使用网格视图创建自定义警报对话框?
【发布时间】:2014-08-17 15:14:17
【问题描述】:

如上图所示,如何使用 GridView 创建一个警报对话框

【问题讨论】:

  • 查看我的编辑答案

标签: android android-alertdialog android-gridview


【解决方案1】:

这里是一个简单的实现:在你的代码里面调用这个方法。

private void showAlertDialog() {
        // Prepare grid view
        GridView gridView = new GridView(this);

        List<Integer>  mList = new ArrayList<Integer>();
        for (int i = 1; i < 36; i++) {
            mList.add(i);
        }

        gridView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, mList));
        gridView.setNumColumns(5);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // do something here
            }
        });

        // Set grid view to alertDialog
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(gridView);
        builder.setTitle("Goto");
        builder.show();
    }

【讨论】:

  • 非常感谢@Vasudev,它确实有效。我整天都在寻找这个。非常感谢
【解决方案2】:

您可以使用弹出窗口

  import android.widget.PopupWindow;


        private PopupWindow mpopup;

    // getting the layout of the popup view . in this case it is about.xml
    final View popUpView = getLayoutInflater().inflate(R.layout.about, null,false);


                 mpopup = new PopupWindow(popUpView, 400, 500, true); // here 400 and 500 is the height and width of layout
                 mpopup.setAnimationStyle(android.R.style.Animation_Dialog);  
                 //location of popup view on the screen
                 mpopup.showAtLocation(popUpView, Gravity.CENTER, 0, 0);


        // if you have button in the xml file of about.xml
        Button cancel=(Button)popUpView.findViewById(R.id.close1);
        cancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
               // to dismiss popup();
                mpopup.dismiss();

            }
        });

R.layout.about 是一个 xml 文件,您可以在其中放置网格视图和其他内容

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-13
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    • 2021-04-19
    • 2020-01-27
    相关资源
    最近更新 更多