使RatingBar辅助颜色不透明

时间:2018-09-07 00:54:48

标签: android ratingbar

我正在尝试使RatingBar中的未填充星星变成不透明的颜色,但这似乎是一项艰巨的任务,包括色调列表和PorterDuff模式。是否有坚如磐石的方法将RatingBar的未填充星星变成真正不透明的颜色?

val color = ContextCompat.getColor(context, android.R.color.white)
ratingBar.secondaryProgressTintList = ColorStateList.valueOf(white)
ratingBar.secondaryProgressTintMode = PorterDuff.Mode.OVERLAY //whats the sauce?

未填充的颜色仍然是透明的,视图的背景是一半白色!

2 个答案:

答案 0 :(得分:6)

尝试使用这种布局来评价星级

  

编译'com.iarcuschin:simpleratingbar:0.1.5'

<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:bind="http://schemas.android.com/tools">

<data>

    <import type="android.view.View" />

    <variable
        name="feedbackHandler"
        type="id.paypro.ui.handlers.FeedbackHandler"></variable>
</data>

<LinearLayout
android:id="@+id/parent_view"
android:orientation="vertical"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:background="@color/white"
    android:contentInsetLeft="0dp"
    android:contentInsetStart="0dp"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    android:contentInsetRight="0dp"
    android:contentInsetEnd="0dp"
    app:contentInsetRight="0dp"
    app:contentInsetEnd="0dp"
    android:elevation="2dp"
    android:layout_height="?attr/actionBarSize">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.AppCompatImageView
            android:id="@+id/imgBack"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:onClick="@{feedbackHandler::onClickBack}"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_back_black"/>

        <TextView
            android:id="@+id/title_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/feedback"
            android:layout_marginLeft="15dp"
            android:textColor="@color/black"
            android:textSize="20sp"
            style="@style/NormalView"
            android:layout_toRightOf="@+id/imgBack"
            android:layout_centerVertical="true"/>

    </RelativeLayout>

</android.support.v7.widget.Toolbar>

<View
    android:layout_width="match_parent"
    android:visibility="gone"
    android:background="@color/gray"
    android:layout_height="1dp"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:gravity="center"
        style="@style/NormalView"
        android:textSize="20sp"
        android:text="@string/rate_paypro"/>

    <com.iarcuschin.simpleratingbar.SimpleRatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        app:srb_stepSize="1.0"
        app:srb_numberOfStars="5"
        android:layout_gravity="center"
        style="?android:attr/ratingBarStyle"
        android:layout_marginTop="10dp"
        android:isIndicator="true"
        app:srb_fillColor="@color/darkOrange"
        app:srb_borderColor="@color/gray"
        android:layout_height="wrap_content" />

</LinearLayout>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/black"
    android:gravity="start"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="20dp"
    style="@style/NormalView"
    android:textSize="18sp"
    android:text="@string/share_feedback"/>

<EditText
    android:id="@+id/edv_feedback"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_width="match_parent"
    android:hint="@string/write_feedback"
    android:layout_marginTop="5dp"
    android:padding="7dp"
    android:maxLines="5"
    android:maxLength="500"
    android:background="@drawable/btn_gray_border"
    android:gravity="start"
    android:textSize="15sp"
    android:layout_height="80dp" />

<TextView
    android:id="@+id/btn_submit"
    android:layout_width="match_parent"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_height="45dp"
    android:textSize="20sp"
    android:gravity="center"
    style="@style/NormalView"
    android:enabled="false"
    android:alpha="0.6"
    android:onClick="@{feedbackHandler::onClickSubmit}"
    android:layout_marginTop="18dp"
    android:background="@drawable/button_orange_dark"
    android:textColor="@color/white"
    android:text="@string/submit"/>


</LinearLayout>

</layout>

根据触摸显示突出显示的代码:

 rateBinding.ratingBar.setOnRatingBarChangeListener(new 
 SimpleRatingBar.OnRatingBarChangeListener() {
        @Override
        public void onRatingChanged(SimpleRatingBar simpleRatingBar, float rating, 
  boolean fromUser) {
            if (rating > 0) {
                ratePayproBinding.btnSubmit.setEnabled(true);
                ratePayproBinding.btnSubmit.setAlpha(1);
            } else {
                ratePayproBinding.btnSubmit.setEnabled(false);
                ratePayproBinding.btnSubmit.setAlpha(0.6f);
            }
        }
    });

答案 1 :(得分:3)

您可以通过创建自定义可绘制资源文件来实现此目的,

在名称为id的可绘制目录中创建新文件并粘贴此代码

ensure_future

然后在评分栏中添加属性:

rating_state.xml