Android透明DialogFragment

时间:2014-10-02 07:32:37

标签: android android-alertdialog android-dialogfragment

我有一个对话框片段,在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;
        }   
    }
}

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

马库斯

4 个答案:

答案 0 :(得分:9)

将以下行添加到onCreateDialog

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

答案 1 :(得分:3)

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

答案 2 :(得分:1)

DialogFragment

中试试这个
getWindow().setBackgroundDrawable(new ColorDrawable(0));

答案 3 :(得分:1)

试试这个代码段:

true