在Android Java中设置最小屏幕亮度:
final WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
getWindow().setAttributes(lp);要在Android Kotlin中更改屏幕亮度:
val lp = this.window.attributes
lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF
this.window.attributes = lp然而(作为Kotlin的初学者),我很惊讶地看到这一行也行得通:
window.attributes.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF为什么这是可行的?这段代码有没有什么不想要的副作用?
发布于 2022-01-05 11:39:05
在Kotlin中,更新活动中单个参数的最短方法如下所示:
window.attributes = window.attributes.apply {
screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF
}这是因为您需要从android.view.getAttributes()获取当前的LayoutParams,更新screenBrightness值,然后使用更新后的LayoutParams调用android.view.setAttributes()。
https://stackoverflow.com/questions/52569632
复制相似问题