自定义警报对话框

时间:2014-07-27 13:09:28

标签: android alertdialog android-alertdialog

我有一个片段,会自动显示一个对话框。我想更改对话框标题的颜色和按钮的颜色(按下时)。我有什么方法可以做到吗?

public class MyFragment extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        //...
        AlertDialog ad = new AlertDialog.Builder(getActivity()).setTitle("Title")
                .setMessage("Message")
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        //YES logic
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
            //NO logic}
        }).create();
            ad.show();
            inflater.inflate(R.layout.fragment_my, container);
            return super.onCreateView(inflater, container, savedInstanceState);
    }

1 个答案:

答案 0 :(得分:1)

您可以创建自己的Dialog类,扩展Android Dialog,然后您可以自定义其背景可绘制,自定义其标题等。

public class MyDialog extends Dialog {

    public MyDialog(Context context) {
        // Adding your custom theme to it
        super(context, R.style.mydialog_theme);
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Removing its original title bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        // Getting reference to its window object
        Window window = getWindow();

        // Where in the screen your dialog pops up
        window.setGravity(Gravity.CENTER);

        // Setting its content view 
        setContentView(R.layout.mydialog_layout);
    }
}

因此,您可以创建如下对话框:
enter image description here