Dim background of an activity

给你一囗甜甜゛ 提交于 2019-12-24 01:55:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!