使用AlertDialog在DialogFragment上正确应用主题

时间:2012-08-17 05:41:50

标签: android alertdialog

我似乎无法在DialogFragment / AlertDialog上正确应用主题。

以下是代码:

protected static class TabRenameDialogFragment extends DialogFragment
{

    public static TabRenameDialogFragment newInstance(long tabId, CashierActivity ca) {

        // create new dialogfragment and initialize items
        TabRenameDialogFragment trdf = new TabRenameDialogFragment();

        Bundle b = new Bundle();
        b.putLong("TAB_ID", tabId);

        trdf.setArguments(b);

        return trdf;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final DialogFragment fm = this;

        // set data
        final long id = getArguments().getLong("TAB_ID");

        return new AlertDialog.Builder(getActivity(), R.style.Theme_Styled_Dialog)
                // .setIcon(R.drawable.alert_dialog_icon)
                .setTitle("Rename " + cTabName)
                .setView(view)
                .setCancelable(true)
                .setPositiveButton("Set",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                        }
                    }
                ).setNegativeButton("Cancel", 
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            // nothing!
                        }
                    })
                .create();
    }

}

我正在使用AlertDialog.Builder()的2参数构造函数来设置主题;它正确设置主题,但在dialogfragment本身周围出现一个奇怪的边框。我认为这是因为我主题是DialogFragment中的AlertDialog,但我不知道如何摆脱外部部分。我已经尝试将DialogFragment上的样式设置为STYLE_NO_FRAME等。

Dialog Bug

提前致谢。

修改

以下是我正在应用的样式:

<style name="Theme.Styled.Dialog" parent="@style/Theme.Sherlock.Light.Dialog">
    <item name="dropDownListViewStyle">@style/Widget.Styled.DropDownListStyle</item>
    <item name="android:dropDownListViewStyle">@style/Widget.Styled.DropDownListStyle</item>

    <item name="android:windowTitleStyle">@style/DialogWindowTitle.Styled</item>

    <item name="android:textColorPrimary">@color/bg_color_main</item>
    <item name="android:textColorPrimaryInverse">@color/bg_color_main_dark</item>
</style>

<style name="DialogWindowTitle.Styled" parent="@style/DialogWindowTitle.Sherlock.Light">
    <item name="android:textAppearance">@style/TextAppearance.Styled.DialogWindowTitle</item>
</style>

<style name="TextAppearance.Styled.DialogWindowTitle" parent="@style/TextAppearance.Sherlock.Light.DialogWindowTitle">
    <item name="android:textColor">@color/bg_color_main</item>
</style>

3 个答案:

答案 0 :(得分:0)

一个选项是覆盖windowBackground样式,如下所示:

<item name="android:windowBackground">@drawable/dialog_background</item>

颜色定义如下:

<color name="transparent_white">#00FFFFFF</color>

这仍然不理想,因为如果使用默认样式,生成的窗口仍然稍微窄一些。

答案 1 :(得分:0)

不是返回从AlertDialog.builder获得的Dialog,而是获取DIalog并删除背景:

Builder builder = new AlertDialog.Builder(...);
Dialog dialog = builder.create();
dialog.requestWindowFeature(STYLE_NO_TITLE);
return dialog;

我没有经过测试,但它应该工作(如果没有,请告诉我)。

答案 2 :(得分:0)

尝试以下方法;

ContextThemeWrapper context = new ContextThemeWrapper(getActivity(), android.R.style.Theme_Holo_Light_Dialog);
AlertDialog.Builder builder = new AlertDialog.Builder(context);