【问题标题】:Make the RatingBar secondary color opaque使 RatingBar 辅助颜色不透明
【发布时间】:2018-09-07 00:54:47
【问题描述】:

我正在尝试将 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?

未填充的颜色还是透明的,view的背景是半白的!

【问题讨论】:

  • 尝试使用 MULTIPLY 或 ADD。
  • 另外,我认为您想设置进度背景颜色,而不是辅助进度颜色。
  • 要添加的 progressBackgroundTintMode 给它一个完全白色的背景,对于 MULTIPLY,它什么都不做。

标签: android ratingbar


【解决方案1】:

RatingBar 中的星星只是 LayerDrawable。设置未填充星的颜色的关键是获取LayerDrawable 中索引的句柄,该索引包含对未填充星的可绘制对象的描述(索引 = 0)。一旦我们有了这个drawable,我们可以用"Drawable#setTint()"为API 21+或"Drawable#setColorFilter()"为API

以下示例将未填充的星星设置为不透明的绿色:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RatingBar ratingBar = findViewById(R.id.ratingBar);
        LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
        int unfilledColor = getResources().getColor(android.R.color.holo_green_dark);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            stars.getDrawable(0).setTint(unfilledColor);
        } else {
            stars.getDrawable(0).setColorFilter(unfilledColor, PorterDuff.Mode.SRC_ATOP);
        }
    }
}

【讨论】:

    【解决方案2】:

    试试这个布局来评星

    编译'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);
                }
            }
        });
    

    【讨论】:

      【解决方案3】:

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

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

      <?xml version="1.0" encoding="utf-8"?>
      <layer-list xmlns:android="http://schemas.android.com/apk/res/android">  
          <item android:id="@android:id/background" android:drawable="@drawable/blank_star" /> <!-- here is drawable you should add blank image -->
          <item android:id="@android:id/secondaryProgress" android:drawable="@drawable/blank_star" />
          <item android:id="@android:id/progress" android:drawable="@drawable/full_star" /> <!-- here is drawable you want to show when rating selected -->
      </layer-list>
      

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

      android:progressDrawable="@drawable/rating_state"
      

      【讨论】:

        【解决方案4】:

        我认为this 可能是您要查找的库。

        例子:

        <com.iarcuschin.simpleratingbar.SimpleRatingBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srb_borderColor="@android:color/transparent"
            app:srb_fillColor="@color/colorAccent"
            app:srb_numberOfStars="5"
            app:srb_rating="3"
            app:srb_starBackgroundColor="@color/colorPrimary"
            app:srb_starSize="40dp"
            app:srb_stepSize="0.5" />
        

        结果:

        视图可以配置如下:

        • 在正常状态下用app:srb_fillColor/setFillColor(@ColorInt int)设置星星填充颜色
        • app:srb_starBackgroundColor/setStarBackgroundColor(@ColorInt int)设置正常状态下的星星背景颜色
        • 使用app:srb_pressedFillColor/setPressedFillColor(@ColorInt int)在按下状态下设置星星填充颜色
        • 使用app:srb_pressedStarBackgroundColor/设置按下状态下的星星背景颜色/ setPressedStarBackgroundColor(@ColorInt int)

        更多配置选项请点击here

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-06
          • 2016-04-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-09-02
          相关资源
          最近更新 更多