簡體   English   中英

如何在android中創建帶網格視圖的自定義警報對話框?

[英]How can I create custom alert dialog with grid view in android?

在此輸入圖像描述

如何使用GridView創建警報對話框 ,如上圖所示?

這是一個簡單的實現:在代碼內部活動中調用此方法。

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

您可以使用彈出窗口

  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文件,你可以將你的網格視圖放在里面和其他東西

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM