无法使我的DialogFragment背景透明
我已经创建了一个自定义的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中设置自定义样式,其定义如下:
@null true true true false @null
我也试过getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0));
这导致空指针exception。
我正在使用android.support.v4.app.DialogFragment
。 这可能是原因吗? 或者我做错了什么?
如果您需要对话框的屏幕截图,请告诉我们! 谢谢
尝试在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类,则还可以在对话框实例上设置样式和主题。
这对我有用。 我创造了一种新风格:
然后在我的DialogFragment的onCreate()
方法中设置此样式,如下所示:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setStyle(DialogFragment.STYLE_NO_TITLE, R.style.CustomDialog); }
这是我用来完全删除Dialog背景的样式.-
对于Dialog
创建,你创建了一个DialogBuilder
但是你手动给视图充气,我想这就是问题所在。 试试这个.-
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.customTheme)); AlertDialog dialog = builder.create(); dialog.show();
编辑
另一种方法是扩展AlertDialog
.-
public class CustomDialog extends AlertDialog { public DialogParent(Context context) { super(context, R.style.CustomDialog); setContentView(R.layout.pausedialog); // More initialization stuff } }
然后’手动’实例化它.-
AlertDialog dialog = new CustomDialog(getActivity()); dialog.show();