活动的昏暗背景 [英] Dim background of an activity
问题描述
我正在尝试将活动显示为背景暗淡的浮动窗口,并且我使用以下代码来实现此目的,该代码取自Google I/O 2016项目:
I am trying to show an activity as a floating window with a dimmed background and I'm using the following code to do that, which is taken from the Google I/O 2016 project:
protected void setupFloatingWindow(int width, int height, int alpha, float dim) {
WindowManager.LayoutParams params = getWindow().getAttributes();
params.width = getResources().getDimensionPixelSize(width);
params.height = getResources().getDimensionPixelSize(height);
params.alpha = alpha;
params.dimAmount = dim;
params.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
getWindow().setAttributes(params);
}
我在super.onCreate和setContentView之前的onCreate中调用此函数,就像在Google I/O示例代码中一样.
I am calling this function in onCreate, before super.onCreate and setContentView , as it is in the Google I/O example code.
这样,它对我不起作用.该活动显示为一个浮动窗口,具有我设置的大小,但是后面的背景是黑色的,并且不像我期望的那样暗淡.我要设置的dimAmount为0.4f.
This way it doesn't work for me. The activity is shown as a floating window, with the size that I am setting, but the background behind is black and not dimmed as I expect it to be. The dimAmount that I am setting is 0.4f.
我错过了什么吗?我应该再添加些东西吗?请帮助
Am I missing something? Should I add something more? Please help
推荐答案
要尝试的两件事:
params.alpha
值是一个浮点数,应在0.0到1.0之间,但是您要传递一个int值,因此alpha值将设置为完全不透明或完全透明,而中间没有任何内容.
The params.alpha
value is a float that should be between 0.0 and 1.0, but you are passing an int, so the alpha value gets set to fully opaque or fully transparent, nothing in between.
此外,在将活动样式更改为包括以下内容后,我只能在模拟器上运行此功能:
Also, I was only able to get this working on my emulator after changing the activity style to include:
<item name="android:windowIsTranslucent">true</item>
我不知道您正在使用的项目中是否设置了该值.
I don't know if that's set in the project you are working from or not.
这篇关于活动的昏暗背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!