问题
I have created a custom DialogFragment like it is described in the developer guide.
Now what i am trying to do sounds simple enough, but i cannot get it to work.
I have defined: android:background="@android:color/transparent"
in my layout xml wich i am loading like this (in my 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);
As you can see i also tried to set a custom style in the DialogFragment wich is defined like this:
<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>
And i also tried getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0));
which leads to a null pointer exception.
I am using android.support.v4.app.DialogFragment
. Can this be the cause?
Or am i doing something else wrong?
If you need a screenshot of the dialog please let me know! Thanks
回答1:
Try to set style and theme in onCreate method of your dialogFragment class implementation.
@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);
}
Or
if you are using Dialog class then you can also set Style and theme on dialog instance.
回答2:
This worked for me. I created a new style:
<style name="CustomDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
</style>
And then set this style in my DialogFragment's onCreate()
method like this:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.CustomDialog);
}
回答3:
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();
来源:https://stackoverflow.com/questions/19167185/cant-get-my-dialogfragment-background-to-be-transparent