繁体   English   中英

如何在 Android 中以编程方式为 RatingBar 设置主题

[英]How to set theme for a RatingBar programmatically in Android

我的布局文件中有一个这样的RatingBar

<RatingBar
    android:id="@+id/ratingBar"
    style="?android:attr/ratingBarStyleIndicator"
    android:theme="@style/MyRatingBar"
    ... />

它按我的预期工作,现在我需要在我的 Java 代码中动态创建它,并为它设置样式,如下所示:

RatingBar ratingBar = new RatingBar(getApplicationContext(), null, android.R.attr.ratingBarStyleIndicator);

但我不知道如何为此设置主题MyRatingBar

那里的解决方案使用AppCompatRatingBar只需根据您的条件为评级栏的进度颜色着色,无需在其上设置主题

ratingbar.setProgressTintList(ColorStateList.valueOf(ContextCompat.getColor(context, R.color.colorAccent)));

使用ContextThemeWrapper

RatingBar ratingBar = new RatingBar(new ContextThemeWrapper(mContext, R.style.Theme_AppCompat));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM