在Android 11设备上的RemoteViews中更改ProgressBar颜色

问题描述

我试图更改用RemoteViews中的形状创建的ProgressBar的颜色。它在我的手机上效果很好,但是在Android 11设备上,ProgressBar颜色不变。 问题发生在使用Android 11的Pixel 4xl上。

要实现此目的,我使用了反射API,因为here显示了Oleksandr Albul。我将PreferenceScreen与Martin Stone制造的ColorPicker一起使用

我想要得到什么: the effect I want to get

我在Android 11设备上会得到什么: 在认值中设置了蓝色和浅灰色 the effect I get

我的代码

小部件布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/widget_padding"
tools:context=".MainActivity">

<ImageView
    android:id="@+id/widget_background"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:adjustViewBounds="true"
    android:src="@drawable/widget_background" />

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

    <ProgressBar
        android:id="@+id/widget_progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:indeterminate="false"
        android:max="1440"
        android:progressDrawable="@drawable/widget_circle_shape" />

        <TextView
            android:id="@+id/widget_timeText"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
           android:textSize="20sp"
            android:textColor="@color/progressbar_blue"
            android:textStyle="bold"
            />

</FrameLayout>

ProgressBar形状:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape
            android:innerRadiusRatio="2.5"
            android:shape="ring"
            android:thicknessRatio="20"
            android:useLevel="false"
            >

            <solid android:color="@color/progressbar_grey" />

        </shape>

    </item>

    <item android:id="@android:id/progress">

        <rotate
            android:fromdegrees="270"
            android:todegrees="270">
            <shape
                android:innerRadiusRatio="2.5"
                android:shape="ring"
                android:thicknessRatio="20"
                android:useLevel="true"><!-- this line fixes the issue for lollipop api 21 -->

                <solid android:color="@color/progressbar_blue" />
            </shape>
        </rotate>
    </item>
</layer-list> 

小部件活动:

static void updateAppWidget(Context context,AppWidgetManager appWidgetManager,int appWidgetId) {

    Log.d(TAG,"updateWidget called");
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget);

    Intent intent = new Intent(context,TimeUpdateService.class);
    intent.setAction(TimeUpdateService.ACTION_UPDATE);
    PendingIntent pendingIntent = PendingIntent.getService(context,intent,PendingIntent.FLAG_UPDATE_CURRENT);

    remoteViews.setonClickPendingIntent(R.id.widget_timeText,pendingIntent);
    remoteViews.setonClickPendingIntent(R.id.widget_progressBar,pendingIntent);

    int currentTime = TimeUtils.getCurrentTime(context);
    remoteViews.setTextViewText(R.id.widget_timeText,String.valueOf(currentTime));
    remoteViews.setProgressBar(R.id.widget_progressBar,1440,currentTime,false);


    if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean(PrefrenceKey.SHOW_BACKGROUND_SHAPE,true)) {
        remoteViews.setInt(R.id.widget_background,"setVisibility",View.VISIBLE);

        int color = PreferenceManager.getDefaultSharedPreferences(context).getInt(PrefrenceKey.WIDGET_BACKGROUND_COLOR,R.color.progressbar_grey);
        remoteViews.setInt(R.id.widget_background,"setColorFilter",color);
        remoteViews.setInt(R.id.widget_background,"setAlpha",Color.alpha(color));

    } else {
        remoteViews.setInt(R.id.widget_background,View.GONE);
    }

    //WIDGET PROGRESSBAR PRIMARY COLOR
    Method setTintColor = null;
    try {
        setTintColor = RemoteViews.class.getmethod("setProgresstintList",int.class,ColorStateList.class);
    } catch (SecurityException e) {
        e.printstacktrace();
    } catch (NoSuchMethodException e) {
        e.printstacktrace();
    }
    if (setTintColor != null) {
        try {
            int color = PreferenceManager.getDefaultSharedPreferences(context).getInt(PrefrenceKey.WIDGET_PRIMARY_PROGRESSBAR_COLOR,R.color.progressbar_blue);
            setTintColor.invoke(remoteViews,R.id.widget_progressBar,ColorStateList.valueOf(color));
        } catch (illegalaccessexception e) {
            e.printstacktrace();
        } catch (InvocationTargetException e) {
            e.printstacktrace();
        }
    }

    //WIDGET SECONDARY PROGRESSBAR COLOR
    Method setTintBackgroundColor = null;
    try {
        setTintBackgroundColor = RemoteViews.class.getmethod("setProgressBackgroundTintList",ColorStateList.class);
    } catch (SecurityException e) {
        e.printstacktrace();
    } catch (NoSuchMethodException e) {
        e.printstacktrace();
    }
    if (setTintBackgroundColor != null) {
        try {
            int color = PreferenceManager.getDefaultSharedPreferences(context).getInt(PrefrenceKey.WIDGET_SECONDARY_PROGRESSBAR_COLOR,R.color.progressbar_grey);
            setTintBackgroundColor.invoke(remoteViews,ColorStateList.valueOf(color));
        } catch (illegalaccessexception e) {
            e.printstacktrace();
        } catch (InvocationTargetException e) {
            e.printstacktrace();
        }
    }


    //WIDGET TEXT COLOR
    int textColor = PreferenceManager.getDefaultSharedPreferences(context).getInt(PrefrenceKey.WIDGET_TEXT_COLOR,R.color.progressbar_blue);
    remoteViews.setTextColor(R.id.widget_timeText,textColor);

    //WIDGET TEXT SIZE
    int textSize = Integer.valueOf(PreferenceManager.getDefaultSharedPreferences(context).getString(PrefrenceKey.WIDGET_TEXT_SIZE,"20"));
    remoteViews.setTextViewTextSize(R.id.widget_timeText,COMPLEX_UNIT_SP,textSize);
    Log.d(TAG,"widget text size" + textSize);


    appWidgetManager.updateAppWidget(appWidgetId,remoteViews);


}

我没有Android 11设备上的Logcat。

您知道如何解决吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...