无法使自定义DialogFragment在Fragment上透明


98

我需要在一个片段上创建一个对话框(占用整个屏幕)。该对话框必须是一个浮动对话框,将其放置在片段上方,而片段在片段外部变暗。

对于自定义对话框,无论我做什么,我都有一个具有弯曲边缘的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个小时,希望其他人能帮忙...

Answers:


302

尝试

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

在你DialogFragmentonCreateView


5
您可能还想删除半透明的黑色背景(变暗),请检查以下答案:stackoverflow.com/a/33800422/2115904
Andriy Bas

4
也会删除所有边距。对话框将扩展为全宽。
Uday

1
它将导致异常:java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window android.app.Dialog.getWindow()' on a null object reference
CoolMind

1
您也可以致电getDialog().getWindow().setBackgroundDrawableResource(android.R.color.transparent);。为了避免调用异常,您应该DialogFragment从Activity或Fragment Through dialogFragment.show(...);方法而不是FragmentTransaction的调用add
CoolMind

2
如果有人正在寻找Kotlin代码段,则为:dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
弗朗西斯·拉克莱

24

试试这个(如何创建一个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();

15

像这样设置您的主题对我有用

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

11

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


8

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

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

在样式文件中覆盖属性: android:windowBackground@ color / DialogBackgroundBlackSemiTransparent


7

您可以通过在Dialog FragmentBottomSheetDialogFragment中添加它来实现

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

setBackgroundDrawableResourceclearFlags为我工作(kotlin,android api v28)
Wesely

6
<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);
    }

4

那些使用AlertDialog构建器onCreateDialog代替的人onCreateView可以分配主题,例如以下代码。完整的主题集可以从R.style中找到。别忘了其中的某些功能最近受支持,并且在旧设备的电话上不可用。

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

谢谢,这就是我要寻找的。
Milad Faridnia '16

3

如果您愿意,请尝试以下操作:

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

0

每个接受的答案,用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
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.