How to dim a screen on click of a button in android?

拜拜、爱过 提交于 2019-12-12 10:16:03

问题


How do i darken/dim the current screen on click of a button. PLease help me.


回答1:


Here's one solution, though it may not be the best:

create styles.xml under res/values. Add the following code:

<style name="Theme.Translucent" parent="android:Theme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@color/cache_color</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">true</item>
 </style>

Note that @color/cache_color is <color name="cache_color">#00000000</color>. Now clicking your custom button should send an intent to an activity (e.g., FooActivity). So declare this in your manifest:

<activity android:name="com.FooActivity" android:theme="@style/Theme.Translucent"></activity>

and voila, the screen is dimmed when your button's listener is called!




回答2:


WindowManager.LayoutParams lparams = getWindow().getAttributes();  
lparams.dimAmount=1.0f; 
dialog.getWindow().setAttributes(lparams);  

The dim amount 0 means no dimming, and the dim amount 1.0f means complete dimming. Any value in between is the corresponding percentage of dim.

Just add this code to the button you want.



来源:https://stackoverflow.com/questions/10810575/how-to-dim-a-screen-on-click-of-a-button-in-android

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