【问题标题】:How to dim a screen on click of a button in android?如何在单击android中的按钮时使屏幕变暗?
【发布时间】:2012-05-30 14:01:24
【问题描述】:

如何在单击按钮时使当前屏幕变暗/变暗。请帮我。

【问题讨论】:

标签: android button screen-brightness


【解决方案1】:
WindowManager.LayoutParams lparams = getWindow().getAttributes();  
lparams.dimAmount=1.0f; 
dialog.getWindow().setAttributes(lparams);  

调光量0表示不调光,调光量1.0f表示完全调光。介于两者之间的任何值都是相应的暗淡百分比。

只需将此代码添加到您想要的按钮。

【讨论】:

  • @SherazKhilji 在设置 dimAmount 之前调用 dialog.setContentView
  • @jjyao 我已经解决了这个问题。问题是当我调用它时我在后台线程中,但是一旦我将这段代码移动到 UI 线程中,它就可以工作了。谢谢
【解决方案2】:

这里有一个解决方案,虽然它可能不是最好的:

在 res/values 下创建 styles.xml。添加以下代码:

<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>

请注意,@color/cache_color&lt;color name="cache_color"&gt;#00000000&lt;/color&gt;。现在单击您的自定义按钮应该向活动(例如,FooActivity)发送一个意图。所以在你的清单中声明:

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

瞧,当你的按钮的监听器被调用时,屏幕变暗了!

【讨论】:

  • 这是虚假调光,不是正确的解决方案。
猜你喜欢
  • 1970-01-01
  • 2021-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-07
相关资源
最近更新 更多