1

我通过继承 DialogFragment 类实现了我自己的 Dialog。

我想更改此默认对话框的背景颜色。

我尝试了几种方法,但似乎都没有。如果您看到下面的 onCreateDialog() 方法,我使用的是自定义对话框样式:

public class ConfirmationDialogFragment extends SherlockDialogFragment {

String mTextMessage = "Would you like to save the message"; 
String mTextButtonOk = "Ok";
TestDialogInterface dialogInterface = null;

public void setDialogInterface(TestDialogInterface dialogInterface) {
    this.dialogInterface = dialogInterface;
}

/**
 */
public ConfirmationDialogFragment() {       
}

public void setTextMessage(String mTextMessage) {
    this.mTextMessage = mTextMessage;
}


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), R.style.AlertDialogCustom));
    builder.setMessage(mTextMessage)
           .setPositiveButton(mTextButtonOk, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   dismiss();
               }
           })
           .setCancelable(false);                
    builder.setTitle("Loan Extension");
    Dialog dialog = builder.create();
    return dialog;
}   

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = super.onCreateView(inflater, container, savedInstanceState);
    return view;        
}

public class TestDialogInterface{
    public void onYes(){}
    public void onNo(){}        
}    

}

样式文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="AlertDialogCustom" parent="@android:style/Theme.DeviceDefault.Light.Dialog">        
        <item name="android:windowBackground">@color/red</item>
        <item name="android:colorBackground">@color/red</item>                              
    </style>
</resources>

但是 windowBackground 样式项似乎没有效果。我需要更改此默认对话框的背景颜色。

有人有线索吗???

4

3 回答 3

2

我建议您创建一个自定义对话框。求助

http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog

或者

http://www.androidpeople.com/android-custom-dialog-example

于 2012-11-28T13:50:49.170 回答
1

更改根视图、线性布局或其他任何内容的背景颜色。

于 2014-11-16T19:51:57.940 回答
0

这个对我有用:

   @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Setup the layout
    LayoutInflater inflater = getActivity().getLayoutInflater();
    final View root = inflater.inflate(R.layout.sample_dialog_fragment, null);
    root.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

    //Customizing the dialog features
    final Dialog dialog = new Dialog(getActivity());
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(root);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.light_gray)));
    dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    return dialog;
}

只需将 sample_dialog_fragment 替换为您的布局,并将 R.color.light_gray 替换为您的颜色。我希望它对你有帮助。

于 2016-04-11T06:42:08.760 回答