无法获得透明的DialogFragment

时间:2013-12-11 19:47:22

标签: android android-dialogfragment

我有一个对话片段,看起来像那样。

enter image description here

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

此代码获得半透明背景。但我仍然在底部有一个白色的部分。我想摆脱白色只是半透明背景

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

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

它可能不是LinearLayout,因为当我增加保证金时,没有任何动静。

这是我的代码:

@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中调用它:

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


}

任何帮助将不胜感激!

3 个答案:

答案 0 :(得分:23)

问题出在默认对话框主题中。根据{{​​3}}和this答案,您可以更轻松地实现目标。

活动应如下所示:

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样式。

结果是(我使用了LinearLayout并且单个TextView): enter image description here

答案 1 :(得分:6)

我希望它可以帮到你

@Override
public void onStart() {
    super.onStart();

    Window window = getDialog().getWindow();
    WindowManager.LayoutParams windowParams = window.getAttributes();
    windowParams.dimAmount = 0.0f; 

    window.setAttributes(windowParams);
}

答案 2 :(得分:1)

以上解决方案均不适合我。我发现工作的是确保我导入了android.support.v7.app.AlertDialog而不是通用的app.alertdialog。

希望这也有助于其他人。