DialogFragment默认背景颜色更改 - Android

时间:2012-11-28 13:30:21

标签: android android-widget android-dialog android-dialogfragment

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

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

我尝试了几种方法,但似乎没有一种方法可行。 如果你看到下面的onCreateDialog()方法,我正在使用自定义对话框Style:

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样式项似乎没有效果。我需要更改此默认对话框的背景颜色。

任何人都有线索????

2 个答案:

答案 0 :(得分:2)

答案 1 :(得分:1)

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