如何在android中创建带网格视图的自定义警报对话框?

时间:2014-08-17 15:14:18

标签: android android-alertdialog android-gridview

enter image description here

如何使用 GridView 创建提醒对话,如上图所示?

2 个答案:

答案 0 :(得分:22)

这是一个简单的实现:在活动代码中调用此方法。

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();
    }

答案 1 :(得分:1)

您可以使用弹出窗口

  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文件,您可以在其中放置网格视图和其他内容

相关问题