如何解决在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 举报,一经查实,本站将立刻删除。