在Android活动中设置全屏亮度 [英] Setting full screen brightness in an Android activity
问题描述
我正在使用此方法将屏幕设置为全亮度.
I'm using this method to set the screen to full brightness.
@SuppressLint("NewApi")
private void setFullBright() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {
WindowManager.LayoutParams windowParams = getWindow().getAttributes();
windowParams.screenBrightness = 1.0f;
getWindow().setAttributes(windowParams);
}
}
如果我希望在Activity屏幕的整个生命周期中设置全部亮度,那么onCreate方法是调用它的最佳位置吗?
If I want the full brightness to be set on the entire life of the Activity's screen, is the onCreate method the best place to call it?
是否存在可以实现此目的的XML标志?像 android:keepScreenOn ="true" 这样的镜像是否可以在代码中添加 WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON 的功能?
Is there an XML flag that can achieve this? Something like android:keepScreenOn="true" that mirrors the functionality of adding WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON in code?
推荐答案
适用于试图在DialogFragment
中实现相同目标的每个人.将参数应用于getActivity().getWindow()
不会有所帮助,因为Activity
的窗口与运行Dialog
的窗口不同.因此,您必须使用对话框的窗口-请参见以下代码片段:
For everyone who's trying to achieve the same in a DialogFragment
. Applying the params to getActivity().getWindow()
won't help since the window of the Activity
is not the same as the window the Dialog
is running in. So you have to use the window of the dialog - see following snippet:
getDialog().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
params.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_FULL;
getDialog().getWindow().setAttributes(params);
并回答原始问题: 没有办法无法通过XML进行设置.
And to answer the original question: No there is no way to set this via XML.
这篇关于在Android活动中设置全屏亮度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!