无法获得透明的DialogFragment [英] Can't get transparent DialogFragment

查看:60
本文介绍了无法获得透明的DialogFragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的对话框片段.

I have a dialog Fragment which look like that.

AlertDialog ad = builder.create();
Drawable d = new ColorDrawable(Color.BLACK);
d.setAlpha(130);
ad.getWindow().setBackgroundDrawable(d);

此代码使背景半透明.但是我在底部仍然有一个白色的部分.我想摆脱白色,只具有半透明的背景

This code get background semi transparent. But I still got a white part on the bottom. I want to get rid of the white to just have semi transparent background

我已经尝试了很多其他帖子中看到的东西.

I already tried a lot of stuff that I saw in other posts.

我不知道在DialogFragment,AlertDialog和LinearLayout之间必须更改的对象是什么.

I don't know what is the object that I must change between the DialogFragment, the AlertDialog and the LinearLayout.

它可能不是LinearLayout,因为当我增加边距时,什么都没有动.

It may not be the LinearLayout, because when I increase margin, nothing is moving.

这是我的代码:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//      setStyle(DialogFragment.STYLE_NORMAL, 0);
//      setStyle(STYLE_NO_FRAME, R.style.CustomDialog);
//      setStyle(STYLE_NO_FRAME, 0);
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    View view = getActivity().getLayoutInflater().inflate(
            R.layout.share_or_die, null);

    AlertDialog ad = builder.create();
    Drawable d = new ColorDrawable(Color.BLACK);
    d.setAlpha(130);
    ad.getWindow().setBackgroundDrawable(d);
    ad.setCanceledOnTouchOutside(true);
    ad.getWindow().requestFeature(Window.FEATURE_NO_TITLE);

    ad.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);      

    return ad;

}

当用户单击后退按钮时,我只是在mainActivity中调用它:

I just call it in the mainActivity when user click back button:

@Override
public void onBackPressed() {
        if (isUserConnected && !hasShared) {
        shareOnExitDialog = new ShareOnExitDialog();
            shareOnExitDialog.setCancelable(true);
            shareOnExitDialog.show(getSupportFragmentManager(), "Exit");
        } else {
            finish();
        }


}

任何帮助将不胜感激!

推荐答案

问题出在默认对话框主题中.基于 this

The issue is in default dialog theme. Based on this and this answers it's much easier to achieve your target.

该活动应如下所示:

public class MyActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
    }

    @Override
    public void onBackPressed() {
        MyDialogFragment.newInstance("title title").show(getSupportFragmentManager(), "dialog");
    }
}

还有片段:

public class MyDialogFragment extends android.support.v4.app.DialogFragment {

    /**
     * Create a new instance of MyDialogFragment, providing "title"
     * as an argument.
     */
    static MyDialogFragment newInstance(String title) {
        MyDialogFragment frag = new MyDialogFragment();
        Bundle args = new Bundle();

        args.putString("title", title);
        frag.setArguments(args);
        return frag;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity(),android.R.style.Theme_Translucent_NoTitleBar);
        final View view = getActivity().getLayoutInflater().inflate(R.layout.share_or_die, null);

        final Drawable d = new ColorDrawable(Color.BLACK);
        d.setAlpha(130);

        dialog.getWindow().setBackgroundDrawable(d);
        dialog.getWindow().setContentView(view);

        final WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.CENTER;

        dialog.setCanceledOnTouchOutside(true);

        return dialog;
    }
}

此外,请确保执行以下操作:在活动中的setContentView()之前,应添加getWindow().requestFeature(Window.FEATURE_NO_TITLE)以便使用*NoTitleBar样式.

Also, be sure to do the following: before setContentView() in the activity, getWindow().requestFeature(Window.FEATURE_NO_TITLE) should be added in order to use *NoTitleBar style.

结果是(我将LinearLayout与单个TextView一起使用):

The result is (I've used LinearLayout with single TextView inside):

这篇关于无法获得透明的DialogFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆