【问题标题】:Theme not applying correctly on DialogFragment with AlertDialog使用 AlertDialog 在 DialogFragment 上未正确应用主题
【发布时间】:2012-08-17 05:41:50
【问题描述】:

我似乎无法在我的 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 中设置了 AlertDialog 的主题,但我不知道如何摆脱外部部分。我已经尝试将 DialogFragment 上的样式设置为 STYLE_NO_FRAME 等。

提前致谢。

编辑:

这是我正在应用的样式:

<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>

【问题讨论】:

  • 您能否更新问题并附上您为对话框定义的样式?
  • 已更新主题,我使用的是ActionBarSherlock,但即使我使用Theme.Holo.Light.Dialog(或任何Holo 对话框主题),问题仍然存在
  • 你找到解决办法了吗?

标签: android android-alertdialog


【解决方案1】:

一个选项是像这样覆盖 windowBackground 样式:

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

颜色定义如下:

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

这仍然不理想,因为与使用默认样式相比,生成的窗口仍然有点窄。

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

      试试下面的;

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-12
        • 1970-01-01
        • 1970-01-01
        • 2012-05-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多