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

如何解决在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.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?