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

如何在android中使用网格视图创建自定义警报对话框?,android,android-alertdialog,android-gridview,Android,Android Alertdialog,Android Gridview,如何使用上图所示的网格视图创建警报对话框 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,fa


如何使用上图所示的网格视图创建警报对话框

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

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

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();
    }
private void showAlertDialog(){
//准备栅格视图
GridView GridView=新的GridView(此);
List mList=new ArrayList();
对于(int i=1;i<36;i++){
M列表添加(i);
}
setAdapter(新的ArrayAdapter(这个,android.R.layout.simple_list_item_1,mList));
gridView.setNumColumns(5);
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
//在这里做点什么
}
});
//将栅格视图设置为alertDialog
AlertDialog.Builder=新建AlertDialog.Builder(此);
builder.setView(gridView);
建造商名称(“Goto”);
builder.show();
}

查看我编辑的答案非常感谢@Vasudev,它真的很有效。我已经找了一整天了。谢谢