android 设置窗口透明效果,android - 如何将对话框窗口背景设置为透明,而不影响其边距...

当前,我有以下对话框,我将对其项目执行扩展/折叠动画。

b69be604f10451f535526c3c42d8a881.png

该对话框是通过以下代码创建的import android.support.v7.app.AlertDialog;

final AlertDialog.Builder builder = new AlertDialog.Builder(activity);

final AlertDialog dialog = builder.setView(view).create();

final ViewTreeObserver vto = view.getViewTreeObserver();

vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

public void onGlobalLayout() {

ViewTreeObserver obs = view.getViewTreeObserver();

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {

obs.removeOnGlobalLayoutListener(this);

} else {

obs.removeGlobalOnLayoutListener(this);

}

// http://stackoverflow.com/questions/19326142/why-listview-expand-collapse-animation-appears-much-slower-in-dialogfragment-tha

int width = dialog.getWindow().getDecorView().getWidth();

int height = dialog.getWindow().getDecorView().getHeight();

dialog.getWindow().setLayout(width, height);

}

});

但是,在执行动画时,这是副作用。

25c85ababdd49e672d4dcc34155e151a.png

请注意,动画后对话框中多余的多余白色区域不是由我们的自定义 View 引起的。它是对话框本身的系统窗口白色背景。

我倾向于使对话框的系统窗口背景变得透明。

final AlertDialog.Builder builder = new AlertDialog.Builder(activity);

final AlertDialog dialog = builder.setView(view).create();

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

尽管不再看到不需要的白色背景,但对话框的原始边距也消失了。 (对话框的宽度现在是全屏宽度)

277266a243abe87b6a2bd88f2bab0ebb.png

如何在不影响利润的情况下使其透明?

最佳答案

有一个很简单的方法可以做到这一点:

您需要“修改”用作Drawable背景的Dialog。这些Dialogs使用InsetDrawable作为背景。

API> = 23

只有SDK API 23+允许您获取由Drawable包装的源InsetDrawable(getDrawable()方法)。有了这个,您可以做任何您想做的事-例如将颜色更改为完全不同的颜色(例如RED或其他颜色)。如果使用这种方法,请记住,包装的Drawable是GradientDrawable而不是ColorDrawable!

API <23

对于较低的API,您的(“优雅”)选项非常有限。

幸运的是,您无需将颜色更改为一些疯狂的值,只需将其更改为TRANSPARENT即可。为此,您可以在setAlpha(...)上使用InsetDrawable方法。InsetDrawable background =

(InsetDrawable) dialog.getWindow().getDecorView().getBackground();

background.setAlpha(0);

编辑(作为Cheok Yan Cheng's注释的结果):

或者您实际上可以跳过转换为InsetDrawable并获得相同的结果。请记住,这样做会导致alpha在InsetDrawable本身而不是Drawable包裹的InsetDrawable上更改。

83acfa774ce11de78409bb589aa8fee5.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值