无法使自定义 DialogFragment 对片段透明

我需要在一个片段(占据整个屏幕)上创建一个对话框。这个对话框需要是一个浮动对话框,它将定位在片段上方,片段在片段外部变暗。.

For the custom Dialog, i have a linearLayout that has curved edges, no matter what i do, the dialog has a black bordering on all sides (very small). I've tried everything to make it transparent and go away (so that all of the dialog is just the linear layout - curved box)

对于 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_ Dialogue 只是一个 LinearLayout,它的 android: back 设置为 # 000000

This is my style for the custom Dialog

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

我尝试了这种风格的各种组合(从我在网上看到的) ,我不能摆脱讨厌的黑色边框,我可以把它画成白色或任何其他颜色,如果我设置的线性布局背景,以任何东西,而不是 # 000000..。

我已经花了3-4个小时在这上面了,我希望有人能帮忙..。

65828 次浏览

试试看

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

in your DialogFragment's onCreateView

如果你愿意,试试这个:

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

试试这个(如何创建一个100% 自定义的对话框片段) 为对话而工作

    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();

那些在 onCreateDialog而不是 onCreateView中使用 AlertDialog 构建块的用户可以像下面的代码那样分配主题。完整的主题集可以从 R 风格中找到。不要忘记其中一些是最近才支持的,并且在旧的设备手机上是不可用的。

@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();
}

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

for DialogFragment transparent

用于完全透明的用途: SetStyle (DialogFragment.STYLE _ NO _ FRAME,android.R.style. Theme _ 半透明 _ NoTitleBar _ Fullscreen) ;

对于自定义背景,在 value 文件夹(value/style.xml)中创建一个样式文件,并使用它: SetStyle (DialogFragment.STYLE _ NO _ FRAME,yourpackagename.R.style.YOURE _ CustOM _ STYLE) ;

在样式文件中覆盖属性: Android: windows 背景 to be < strong >@color/DialogBackoundBlackSemiTransparency

像这样设置你的主题对我来说很管用

<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);
}
}
<style name="BaseDialogTheme" parent="Base.Theme.AppCompat.Light.Dialog">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="colorControlNormal">@color/colorAccent</item>
<item name="colorControlActivated">@color/colorAccent</item>


<item name="android:windowFullscreen">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:colorBackground">@android:color/transparent</item>
<item name="android:windowIsTranslucent">true</item>




<item name="android:windowIsFloating">true</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:windowActionModeOverlay">false</item>
<item name="android:windowCloseOnTouchOutside">true</item>
<item name="android:backgroundDimAmount">.00</item>//this line is changed alpha from 1.0 to 0.0(full transparent)


</style>






@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_FRAME, R.style.BaseDialogTheme);
}

您可以通过添加这个 对话框片段 或 < strong > BottomSheetDialog 片段

onCreateDialog方法

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.getWindow().setGravity(Gravity.BOTTOM);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
return dialog;
}

每个接受的答案,在 Kotlin

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
var v = super.onCreateView(inflater, container, savedInstanceState)
dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
return v
}