1

我创建了一个自定义 DialogFragment,就像开发人员指南中描述的那样。现在我想做的事情听起来很简单,但我无法让它发挥作用。我已经定义:android:background="@android:color/transparent"在我这样加载的布局 xml 中(在我的 onCreateDialog 中):

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

LayoutInflater inflater = getActivity().getLayoutInflater();
    
final View view = inflater.inflate(R.layout.pausedialog, null);
setStyle(STYLE_NO_FRAME, R.style.CustomDialog);

如您所见,我还尝试在 DialogFragment 中设置自定义样式,其定义如下:

<style name="CustomDialog" parent="android:style/Theme.Dialog">
    <item name="android:windowBackground">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowIsTranslucent">true</item> 
    <item name="android:alwaysDrawnWithCache">false</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

而且我还尝试getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0)); 了导致空指针异常的方法。

我正在使用android.support.v4.app.DialogFragment. 这可能是原因吗?还是我做错了什么?

4

3 回答 3

3

尝试在您的 dialogFragment 类实现的 onCreate 方法中设置样式和主题。

@Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
         int style=DialogFragment.STYLE_NO_TITLE;
         int theme=android.R.style.Theme_Translucent;
         setStyle(style, theme);
    }

或者

如果您使用的是 Dialog 类,那么您还可以在对话框实例上设置样式和主题。

于 2013-10-03T20:50:52.620 回答
2

这对我有用。我创造了一种新风格:

<style name="CustomDialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

然后在我的 DialogFragment 的onCreate()方法中设置这种样式,如下所示:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setStyle(DialogFragment.STYLE_NO_TITLE, R.style.CustomDialog);
}
于 2014-05-19T04:36:16.643 回答
1

This is the style I use to totally remove the Dialog`s background.-

<style name="Theme.Dialog" parent="@android:style/Theme.Translucent.NoTitleBar">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:backgroundDimEnabled">true</item>
</style>

As for the Dialog creation, you're creating a DialogBuilder but then you manually inflate a view, I guess that's the problem. Try this instead.-

AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.customTheme));
AlertDialog dialog = builder.create();
dialog.show();

EDIT

Another approach is extending AlertDialog.-

public class CustomDialog extends AlertDialog {
    public DialogParent(Context context) {
        super(context, R.style.CustomDialog);
        setContentView(R.layout.pausedialog);

        // More initialization stuff    
    }
}

And then 'manually' instantiate it.-

AlertDialog dialog = new CustomDialog(getActivity());
dialog.show();
于 2013-10-03T19:11:13.243 回答