无法使自定义的DialogFragment在片段上透明

我需要创build一个片段(占用整个屏幕)的对话框。 对话框需要是一个浮动的对话框,它将被定位在片段之外,并且片段在片段之外变暗。

对于自定义的对话框,我有一个linearLayout有弯曲的边缘,无论我做什么,对话框都有一个黑色边界(非常小)。 我已经尝试了一切,使其透明,并消失(所有的对话框只是线性布局 – 弯曲的框)

对于DialogFragment,这是我的onCreateView

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ LinearLayout layout =(LinearLayout)inflater.inflate(R.layout.custom_dialog, null); LinearLayout item = (LinearLayout)layout.findViewById(R.id.display_item); populateItemData(item, inflater); return layout; } 

custom_dialog只是一个将android:backgroung设置为#000000的LinearLayout

这是我自定义对话框的样式

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

我尝试了这种风格的各种组合(从我在网上看到的),我不能摆脱那种恼人的黑色边界,我可以把它绘成白色或任何其他颜色,如果我设置LinearLayout背景以外的任何东西#000000 …

我已经花了3-4个小时,我希望别人可以帮忙…

尝试

 getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 

在你的DialogFragmentonCreateView

试试这个( 如何创build一个100%定制的DialogFragment )这个工作对话框

  Dialog dialog = new Dialog(getActivity()); dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE); dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // layout to display dialog.setContentView(R.layout.add_edit); // set color transpartent dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); dialog.show(); 

onActivityCreated

 getDialog().getWindow().getAttributes().alpha = 0.9f; // An alpha value to apply to this entire window. An alpha of 1.0 means fully opaque and 0.0 means fully transparent 

DialogFragment透明

对于完全透明的使用: setStyle(DialogFragment.STYLE_NO_FRAME,android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);

对于自定义背景 – 在你的值文件夹(values / style.xml)中创build一个样式文件并使用它: setStyle(DialogFragment.STYLE_NO_FRAME,yourpackagename.R.style.YOURE_CUSTOM_STYLE);

在你的风格文件重写属性: android:windowBackground@ color / DialogBackgroundBlackSemiTransparent

像这样设置你的主题为我工作

 <style name="MyDialog" parent="Base.Theme.AppCompat.Light.Dialog"> <item name="android:windowBackground">@android:color/transparent</item> </style> 

并在你的对话框中设置像这样

 public class Progress extends DialogFragment { int style = DialogFragment.STYLE_NO_TITLE; int theme = R.style.MyDialog; public Progress() { } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setStyle(style, theme); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.progress, container, false); } } 

那些在onCreateDialog而不是onCreateView中使用AlertDialog构build器的人可以像下面的代码一样分配主题。 完整的主题可以从R.style中find。 不要忘记,其中一些最近支持,并不适用于旧设备的手机。

 @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), android.R.style.Theme_Translucent); View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_album, null); builder.setView(view); return builder.create(); } 

试试这个,如果你喜欢:

 public TransparentDialog() { super(); setStyle(STYLE_NO_FRAME, R.style.AppTheme); }