如何使用AlertDialog从DialogFragment中删除奇怪的边框?

时间:2015-04-27 11:16:09

标签: android android-alertdialog android-dialogfragment

我正在尝试使用以下代码实现基于DialogFragment的AlertDialog:

public class AlertDialogFragment extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new AlertDialog.Builder(getActivity())
                .setTitle("title")
                .setMessage("message")
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .create();
    }
}

它运作良好,但我在AlertDialog周围出现了一个奇怪的白色边框:

enter image description here

如何删除此边框(最好是以编程方式)?

更新:我的styles.xml包含以下代码:

<style name="AppBaseTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:alertDialogTheme">@style/MyDialogTheme</item>
</style>        

<style name="MyDialogTheme" parent="@android:style/Theme.Holo.Light.Dialog"></style>

当我删除<item name="android:alertDialogTheme">@style/MyDialogTheme</item>行时,此问题就消失了。但我需要这一行,因为我想自定义AlertDialog。如何解决这种风格?

1 个答案:

答案 0 :(得分:-1)

我刚检查过您的代码,但没有遇到任何问题。 我称这个班级为

   package com.example.test;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.widget.Toast;

public class A extends FragmentActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);



        AlertDialogFragment al=new AlertDialogFragment();
        FragmentManager fm = getSupportFragmentManager();
        al.show(fm, "test");


    }


class AlertDialogFragment extends DialogFragment {
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            return new AlertDialog.Builder(getActivity())
                    .setTitle("title")
                    .setMessage("message")
                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    })
                    .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    })
                    .create();
        }
    }

}