【问题标题】:Android transparent DialogFragmentAndroid透明DialogFragment
【发布时间】:2016-07-12 20:58:42
【问题描述】:

我有一个对话框片段,它在 WidgetConfig 活动运行时显示。该对话框显示用户可以选择一些项目的列表。我希望此对话框是透明的,以便您可以在对话框的背景中看到主屏幕。这是我目前在 WidgetConfig 活动中所做的:

    DialogFragment dialog = new myChooserDialog();  
    getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));        
    dialog.show(getFragmentManager(), "dialog");    

编辑:myChooserDialog 的代码:

public class MyChooserDialog extends DialogFragment{

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        choices = getResources().getStringArray(R.array.city_choices);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());       
        builder.setTitle(getResources().getString(R.string.widget_dialog_chooser_title));
        builder.setPositiveButton(getResources().getString(R.string.widget_dialog_chooser_posBtn), this);
        builder.setNegativeButton(getResources().getString(R.string.widget_dialog_chooser_negBtn), this);   
        builder.setSingleChoiceItems(choices, -1, this);
        return builder.create();
    }


    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which) {
        case DialogInterface.BUTTON_POSITIVE:   
            //doing magic tricks
            break;

        case DialogInterface.BUTTON_NEGATIVE:
            dialog.dismiss();
            break;

        default:
            //more magic
            break;
        }   
    }
}

目前,背景全黑。我在这里做错了什么?

马库斯

【问题讨论】:

标签: android android-alertdialog android-dialogfragment


【解决方案1】:

以下行添加到您的onCreateDialog

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color
            .TRANSPARENT));
    return dialog;
}

【讨论】:

    【解决方案2】:

    在您的 style.xml 文件中写入以下代码。

    <style name="Theme.Transparent" parent="android:Theme">
           <item name="android:windowIsTranslucent">true</item>
           <item name="android:windowBackground">@android:color/transparent</item>
           <item name="android:windowContentOverlay">@null</item>
           <item name="android:windowNoTitle">true</item>
           <item name="android:windowIsFloating">true</item>
           <item name="android:backgroundDimEnabled">false</item>
       </style>
    

    MyChooserDialog() 类中为对话框设置样式。

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        choices = getResources().getStringArray(R.array.city_choices);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), android.R.style.Theme.Transparent);
    

    【讨论】:

    • 不,因为我没有在 onCreate 方法中构建我的对话框。
    • 另外,这给出了一个错误:方法 getWindow() 未定义类型 DialogFragment
    • 另外,android.R.style.Theme.Transparent 给出了这个错误: android.R.style.Theme 的原始类型 int 没有字段透明 它应该是 R.style.Theme_Transparent这为我的对话框提供了白色背景
    【解决方案3】:

    在您的 DialogFragment 中试试这个

    getWindow().setBackgroundDrawable(new ColorDrawable(0));
    

    【讨论】:

    • myChooserDialog 类型的方法 getWindow() 未定义
    • dialog.setBackgroundDrawable(new ColorDrawable(0)); 添加这个而不是 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    • DialogFragment 类型的方法 setBackgroundDrawable(ColorDrawable) 未定义。请在尝试发布答案之前至少尝试代码。
    • dialog.getView().setBackground(new ColorDrawable(0)); 试试这个
    【解决方案4】:

    试试这个 sn-p:

    getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0));
    

    【讨论】:

    • 这会影响整个窗口。也就是说,例如,如果你想在对话框和窗口之间有一个边距,它是不可见的,所以整个窗口都是透明的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-01
    • 2011-12-24
    • 1970-01-01
    相关资源
    最近更新 更多