You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中android:keepScreenOn与visibility=gone优先级:布局设为gone后是否仍保屏?

问题:设置android:keepScreenOn的控件设为visibility=gone后,屏幕是否还会常亮?

布局代码示例

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/main">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_world"
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge"/>

    <GridLayout
        android:id="@+id/myLayout"
        android:background="@android:color/background_dark"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:keepScreenOn="true">
    </GridLayout>

</RelativeLayout>

问题描述

在上述布局中,GridLayout控件设置了android:keepScreenOn="true",如果将该控件的visibility属性改为gone,而父布局main仍保持显示状态,此时屏幕是否还会保持常亮?从技术层面来说,visibility=gone是否会禁用keepScreenOn属性?

回答

  • 最终结论:屏幕不会再保持常亮,visibility=gone会让控件的keepScreenOn属性失去作用。
  • 技术逻辑:Android系统的屏幕常亮判定逻辑是基于可见控件的——只有当前界面中存在可见(visibility=visible)且带有keepScreenOn="true"属性的控件时,系统才会维持屏幕常亮状态。当所有带该属性的控件都处于不可见状态(包括gone或invisible),屏幕就会恢复正常的变暗、熄灭流程。

测试验证

实际测试结果与上述逻辑一致:当带有keepScreenOn属性的GridLayout被设为gone后,屏幕不再保持常亮,会按照系统默认设置正常进入休眠。


内容的提问来源于stack exchange,提问作者Daniel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 20:55:03