问题
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);
}
I am calling this function in onCreate, before super.onCreate and setContentView , as it is in the Google I/O example code.
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
回答1:
Two things to try:
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.
来源:https://stackoverflow.com/questions/43562709/dim-background-of-an-activity