RatingBar 变成了半透明。

13

我正在使用RecyclerView,在每一行中都有RatingBar。 现在,有时候RatingBar的一部分会被错误地绘制。 离开屏幕并返回后,一切又恢复正常。我不知道为什么会发生这种情况,我甚至从RatingBar中删除了任何样式,因此它应该具有默认外观。

这就是它的样子:

enter image description here

已测试 Nexus 6P (Android 7.1.1)。还测试了三星Galaxy J3 (2016)(Android 5.1.1),这里没有问题。

我还添加了

holder.rbRating.requestLayout();

onBindViewHolder() 中。这会稍微减轻问题,但问题仍然存在。当我将一个“坏”的行滚动到屏幕外并被重用时,它看起来很好。

这是行布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingTop="@dimen/main_margin_top"
    android:paddingBottom="@dimen/main_margin_top"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/tvRatingTitle"
        android:text="Czystość wody"
        android:textSize="16sp"
        android:textStyle="bold"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <RatingBar
        android:id="@+id/rbRating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:isIndicator="true"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/tvRatingTitle"
        android:numStars="5"
        android:rating="2.5"
        android:stepSize="0.1" />

    <CheckBox
        android:id="@+id/chkMissing"
        android:text="Zaznacz jeśli nie ma"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/rbRating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

我也尝试过切换到android.support.v7.widget.AppCompatRatingBar,但是并没有什么不同。

编辑:

@Muhib Pirani的解决方案似乎有效,但我有两个小问题:

1)在Nexus 6P上,第一层星星偏移了几个像素(放大后查看):

enter image description here

2)在三星Galaxy J3(2016)上,它看起来像这样:

enter image description here

我可以接受边框,但我希望它们在空星中也是绿色的(不是灰色,背景应该是灰色)。


通过 工具 -> Android -> 布局检查器 捕获视图层次结构,并查看这些 RatingBar 之间的关键差异。一旦找到导致此行为的属性,就更容易找到实际原因。似乎是改变了 alpha 通道,或者某种程度上应用了色调。 - azizbekian
2个回答

5

我也遇到了同样的问题。我必须以编程方式设置颜色到 RatingBar 中,像这样工作:

LayerDrawable layerDrawable = (LayerDrawable) ratingBar.getProgressDrawable();
layerDrawable.getDrawable(2).setColorFilter(ContextCompat.getColor(context, R.color.colorPrimaryDark), PorterDuff.Mode.SRC_ATOP);
layerDrawable.getDrawable(1).setColorFilter(ContextCompat.getColor(context, R.color.colorPrimaryDark), PorterDuff.Mode.SRC_ATOP);
layerDrawable.getDrawable(0).setColorFilter(ContextCompat.getColor(context, R.color.editTextBor), PorterDuff.Mode.SRC_ATOP);//when not selected color.

保持layerDrawable.getDrawable(2)和layerDrawable.getDrawable(1)的颜色相同。

尝试设置layerDrawable.getDrawable(0).setColorFilter(ContextCompat.getColor(context, android.R.color.transparent), PorterDuff.Mode.SRC_ATOP); - Muhib Pirani
然后它看起来像这样:http://imgur.com/a/czcJC(Nexus 6P)和http://imgur.com/a/wUvgH(Samsung Galaxy J3)- 不是我期望的。 - Makalele
我正在Oneplus X设备上检查它,目前运作良好,但评分栏需要一些改进。 - Muhib Pirani
请查看以下链接: https://dev59.com/zlsW5IYBdhLWcg3wHEHZ#37158074 - Muhib Pirani

1
我之前使用原生的评分条时遇到了问题,因此我决定使用MaterialRatingBar。这解决了我的所有问题。
希望对你有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接