如何在AlertDialog中移除分割线

3

我正在使用 android.support.v7.app.AlertDialog,但我无法去除分割线。请问有人能告诉我如何去除它吗?谢谢。

enter image description here

这是我的样式:

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

如果您不设置标题和按钮,则不会看到分隔线。 - OneCricketeer
你可以创建一个自定义警告对话框来代替它。请查看此教程:http://www.mkyong.com/android/android-custom-dialog-example/ - Oğuzhan Döngül
2个回答

1

AlertDialog的分隔线在不同版本的设备上有所不同。我发现,在早期的设备(即Material Design之前)中,分隔线的颜色为灰色,因此它是可见的。但是对于Material Design(即Lollipop)设备,分隔线的颜色是透明的,因此似乎不可见/不存在。

为了使所有设备上的分隔线可见,请明确指定颜色为灰色或其他任何颜色。

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

0
你是否在 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中使用它,但仍然失败了。 - kirinohana
我已经将我的styles.xml与你的进行了比较,发现它们除了windowActionBarwindowNoTitle这两行之外完全相同。我根本没有这些内容,所以你能否尝试将它们注释掉? - MatteoBelfiori
在对它们进行了注释之后,分隔符仍然存在。 - kirinohana
你不需要这样做,因为你使用了自定义布局。 - Mark Delphi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接