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

适配Android 8-13时顶部进度条消失,报E/PhoneWindow错误求解决

问题:Android高版本(API26+)中进度条无法显示

我的应用在Android 2.2-4.3(API8-18)版本里能在顶部显示进度条,但适配Android8-13(API26-33)后进度条消失,调试日志报错:

E/PhoneWindow: Horizontal progress bar not located in current window decor

查看PhoneWindow.java源码,触发该日志的逻辑如下:

final int features = getLocalFeatures();
if (value == PROGRESS_VISIBILITY_ON) {
    if ((features & (1 << FEATURE_PROGRESS)) != 0) {
        if (horizontalProgressBar != null) {
            int level = horizontalProgressBar.getProgress();
            int visibility = (horizontalProgressBar.isIndeterminate() || level < 10000) ?
                    View.VISIBLE : View.INVISIBLE;
            horizontalProgressBar.setVisibility(visibility);
        } else {
            Log.e(TAG, "Horizontal progress bar not located in current window decor");
        }
    }

原Activity的onCreate()中存在以下代码:

getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);

但官方文档显示,PROGRESS_VISIBILITY_ON在API24被弃用,FEATURE_PROGRESS及相关方法从API21起不再支持。

我尝试在主布局的ViewFlipper上方添加ProgressBar,原布局代码:

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

  <LinearLayout
      android:id="@+id/composite_layout"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/ll_without_this_nothing_works"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
      <ViewFlipper
          android:id="@+id/viewflipper"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" >

        <com.example.olib.OWebView
            android:id="@+id/webview"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <com.example.olib.OImageView
            android:id="@+id/imageview"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/exampleGray"
            android:contentDescription="@string/lo_imageview_desc"
            android:src="@drawable/static" />
      </ViewFlipper>
    </RelativeLayout>

  </LinearLayout>
</LinearLayout>

添加的ProgressBar代码:

<ProgressBar
    android:id="@+id/progress_bar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="24dp"
    android:layout_above="@+id/viewflipper"
    android:indeterminate="false"
    android:max="100"
    android:progress="1"
    android:visibility="invisible" />

在Activity中设置该ProgressBar为View.VISIBLE后,仍无法看到进度条,请问如何恢复进度条显示?


解决步骤
  1. 移除废弃的系统进度条代码
    删掉Activity中的getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);,API21之后这套机制已完全废弃,留着只会触发错误日志。

  2. 修正ProgressBar的布局位置
    android:layout_above属性仅在RelativeLayout内部生效,且你的ProgressBar需要放在RelativeLayout里、ViewFlipper的上方。修改RelativeLayout部分代码,让ProgressBar贴顶,ViewFlipper在其下方:

    <RelativeLayout
        android:id="@+id/ll_without_this_nothing_works"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    
      <ProgressBar
          android:id="@+id/progress_bar"
          style="?android:attr/progressBarStyleHorizontal"
          android:layout_width="match_parent"
          android:layout_height="24dp"
          android:layout_alignParentTop="true"
          android:indeterminate="false"
          android:max="100"
          android:progress="1"
          android:visibility="visible" />
    
      <ViewFlipper
          android:id="@+id/viewflipper"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:layout_below="@+id/progress_bar" >
    
        <!-- 内部WebView和ImageView代码保持不变 -->
      </ViewFlipper>
    </RelativeLayout>
    

    这样ProgressBar会固定在RelativeLayout顶部,不会被ViewFlipper覆盖。

  3. 优化ProgressBar的可见性与样式

    • 若需初始隐藏,设置android:visibility="gone",在需要显示时通过代码切换为View.VISIBLE;
    • 高版本系统默认进度条颜色可能与背景融合,可通过android:progressTint="@color/your_custom_color"设置自定义颜色;
    • 若使用Material Design,可替换样式为style="@style/Widget.MaterialComponents.ProgressBar.Horizontal",适配更美观的高版本UI。
  4. 代码中正确控制进度显示
    在Activity中获取ProgressBar实例,在业务场景(如WebView加载、数据请求)中更新进度:

    ProgressBar progressBar = findViewById(R.id.progress_bar);
    // 示例:WebView加载时更新进度
    webView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            progressBar.setProgress(newProgress);
            progressBar.setVisibility(newProgress == 100 ? View.GONE : View.VISIBLE);
        }
    });
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 21:35:15