AlertDialog分隔线未在Android 4.4 Kitkat

时间:2019-04-08 08:44:08

标签: android android-layout android-alertdialog android-theme android-styles

我有包含四个项目(字符串数组)的警报对话框,我想在每个项目和第一个具有不同颜色的分隔线之间添加分隔线,我在android 4.4 kitkat上看到了这样的

enter image description here

这是我的警报对话框代码

AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setTitle(getString(R.string.choose_layout));

            String[] recyclerViewLayouts = getResources().getStringArray(R.array.RecyclerViewLayouts);
            SharedPreferences.Editor editor = sharedPreferences.edit();

            dialog.setItems(recyclerViewLayouts, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int index) {


                    }
                }
            });
            dialog.create();
            dialog.show();

我尝试使用以下代码创建它,但未显示

AlertDialog alertDialog = builder.create();

ListView listView = alertDialog.getListView();

listView.setDivider(new ColorDrawable(Color.GRAY));

listView.setDividerHeight(1);

alertDialog.show();

1 个答案:

答案 0 :(得分:0)

[已解决]

经过几个小时的搜索,我发现问题是我使用了androidx lib androidx.appcompat.app.AlertDialog 的AlertDialog,尚不支持自动添加分隔线,我应该使用 android.app.AlertDialog.Builder ,更改为该分隔符后再次显示

更新后的代码

android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);

            builder.setTitle(getString(R.string.choose_layout));

            String[] recyclerViewLayouts = getResources().getStringArray(R.array.RecyclerViewLayouts);
            SharedPreferences.Editor editor = sharedPreferences.edit();


            builder.setItems(recyclerViewLayouts, (dialog, index) -> {

                }
            });

            android.app.AlertDialog alertDialog = builder.create();
            alertDialog.show();