自定义AlertDialog布局,为方便以后查阅

 

private void showDialog() {
        final String[] strings = getResources().getStringArray(R.array.options);
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext, R.style.dialogTheme);
        View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_layout, null);
        TextView title = (TextView) view.findViewById(R.id.title);
        ListView lv = (ListView) view.findViewById(R.id.lv);
        title.setText(getResources().getString(R.string.title));
        //准备adapter数据
        List> list = new ArrayList>();
        for (int i = 0; i < strings.length; i++) {
            Map map = new HashMap();
            map.put("text", strings[i]);
            list.add(map);
        }
        SimpleAdapter adapter = new SimpleAdapter(mContext, list, R.layout.list_item, new String[] {
            "text"
        }, new int[] {
            R.id.tv
        });
        lv.setAdapter(adapter);

        AlertDialog dialog = builder.create();
        dialog.setCancelable(false);
        // dialog.setView(view);//此方法添加的view只会显示在底部按钮和顶部标题之间
        dialog.show();
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);
        window.setContentView(view); // 加载自定义布局,可完全覆盖dialog窗口

        WindowManager.LayoutParams params = window.getAttributes();
        params.width = 350;
        params.height = 310;
        dialog.getWindow().setAttributes(params);

        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView arg0, View view, int index, long arg3) {
                switch (index) {
                    case 0:
                        Toast.makeText(mContext, strings[0], 0).show();
                        break;
                    case 1:
                        Toast.makeText(mContext, strings[1], 0).show();
                        break;
                    case 2:
                        Toast.makeText(mContext, strings[2], 0).show();
                        break;
                }
            }
        });
    }

 

你可能感兴趣的:(功能实现)