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

Android中ProgressBar在Presentation内无法显示的问题及Theme使用咨询

问题:Presentation中ProgressBar自定义Drawable不显示(依赖Theme属性)

我在XML中设置了以自定义Spinner为Drawable的ProgressBar,布局预览显示正常,但启动Presentation后ProgressBar仅占用宽高却无法显示。

布局代码如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.MainActivity">

    <FrameLayout
        android:id="@+id/rootView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/backgroundImage"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="centerCrop"
            android:src="@drawable/main_screen_bg"
            android:visibility="gone" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white"
            android:gravity="center"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/webViewLoadingContent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical"
                android:visibility="visible">

                <ProgressBar
                    android:id="@+id/progressCircle"
                    android:layout_width="60dp"
                    android:layout_height="60dp"
                    android:layout_gravity="center"
                    android:indeterminate="true"
                    android:indeterminateDrawable="@drawable/theme_progress_circle" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/purchase_order_single_wait_txt"
                    android:textColor="@color/black"
                    android:textSize="@dimen/font_normal_60" />
            </LinearLayout>

            <WebView
                android:id="@+id/webView"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:visibility="gone" />
        </LinearLayout>

    </FrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

对应的自定义Drawable代码:

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="360" >

    <shape
        android:innerRadiusRatio="3"
        android:shape="ring"
        android:thicknessRatio="8"
        android:useLevel="false" >

        <size
            android:height="76dip"
            android:width="76dip" />

        <gradient
            android:angle="0"
            android:endColor="?attr/colorPrimary"
            android:startColor="@android:color/transparent"
            android:type="sweep"
            android:useLevel="false" />

    </shape>

</rotate>

排查后发现问题出在Drawable中通过?attr/colorPrimary引用的主题属性,请问如何在Presentation中正常使用Theme属性?


解决方案

1. 手动传入带App主题的Context初始化Presentation

Presentation默认使用基于Display的系统上下文,不会自动继承宿主Activity的App主题,导致?attr/colorPrimary这类主题属性无法解析为正确的颜色值(可能被解析为透明或系统默认色),最终Drawable无法正常显示。

解决方式是用ContextThemeWrapper包装宿主Context,传入你的App主题,再用这个包装后的Context初始化Presentation:

public class CustomPresentation extends Presentation {
    public CustomPresentation(Context outerContext, Display display) {
        // 传入App主题,确保Drawable能正确解析主题属性
        super(new ContextThemeWrapper(outerContext, R.style.AppTheme), display);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_presentation_layout);
    }
}

2. 给Presentation布局强制指定主题(可选双重保障)

如果仍有异常,可以在Presentation的布局根节点添加android:theme属性,强制布局内所有控件使用指定主题:

<!-- Presentation布局的根节点 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:theme="@style/AppTheme">

    <!-- 你的布局内容 -->
</LinearLayout>

3. 验证主题属性解析是否正常

可以在Presentation的onCreate方法中添加代码,验证主题属性是否被正确解析:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_presentation_layout);

    // 验证colorPrimary是否正确获取
    TypedValue typedValue = new TypedValue();
    getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true);
    int colorPrimary = typedValue.data;
    Log.d("PresentationTheme", "colorPrimary: " + Integer.toHexString(colorPrimary));
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 16:27:27