【问题标题】:How to remove the divider in AlertDialog如何删除 AlertDialog 中的分隔符
【发布时间】:2015-10-24 15:04:30
【问题描述】:

我正在使用 android.support.v7.app.AlertDialog。但是,我无法移除分隔线。谁能告诉我如何移除它?谢谢。

这是我的风格:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="alertDialogTheme">@style/AppTheme.Dialog.Alert</item>
</style>

<style name="AppTheme.Dialog.Alert" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">@color/colorAccent</item>
</style>

这是我的代码:

AlertDialog.Builder builder = new AlertDialog.Builder(mSettingsView.getBaseActivity());
builder.setTitle("Ringtone");
builder.setSingleChoiceItems(list, -1, listener1);
builder.setPositiveButton("OK", listener2);
builder.setNegativeButton("Cancel", listener3);
builder.show();

【问题讨论】:

标签: android android-view android-theme android-dialog


【解决方案1】:

AlertDialog 分隔符在棒棒糖前和棒棒糖设备中有所不同。我发现,在预棒棒糖(预材料设计)设备中,分隔线颜色是灰色。所以它是可见的。但是对于材料设计(棒棒糖)设备,分隔线颜色是透明的,因此看起来不可见/不存在。

要使分隔线在所有设备上可见,请明确将颜色设为灰色或任何其他颜色。

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

AlertDialog alertDialog = builder.create();

ListView listView = alertDialog.getListView();

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

listView.setDividerHeight(1);

alertDialog.show();

【讨论】:

    【解决方案2】:

    您是否在 android.support.v4.app.DialogFragment 中使用 AlertDialog?我总是这样使用它,而且我从来没有在你的屏幕上看到分隔线:

    import android.support.v4.app.DialogFragment;
    import android.support.v7.app.AlertDialog;
    
    public class MyDialogFragment extends DialogFragment {
    
        public static MyDialogFragment newInstance(){
            return new MyDialogFragment;
        }
    
        @NonNull
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            // Get the layout inflater
            LayoutInflater inflater = getActivity().getLayoutInflater();
            // Inflate my custom layout
            View layout = inflater.inflate(R.layout.my_custom_layout, null);
            // Initialize my layout components
            ...
            // Build dialog
            builder.setTitle("TITLE")
                .setView(layout)
                .setPositiveButton("OK", listener)
                .setNegativeButton("Cancel", listener);
            return builder.create();
        }
    }
    

    【讨论】:

    • 感谢您的建议。我尝试在 android.app.DialogFragment 中使用它。但又失败了。
    • 我已经将我的styles.xml 与你的进行了比较,我注意到除了windowActionBarwindowNoTitle 的行之外它们相同。我根本没有,你能试着评论一下吗?
    • 评论后,分隔线依然存在。
    • 你没有,因为你使用自定义布局。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    • 1970-01-01
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多