使 RatingBar 辅助颜色不透明
Make the RatingBar secondary color opaque
我正在尝试将 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?
未填充的颜色仍然是透明的,视图的背景是半白的!
您可以通过创建自定义可绘制资源文件来实现此目的,
在 drawable 目录中创建名称为 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"
试试这个布局来评星级
compile '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);
}
}
});
我认为 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" />
结果:
The view can be configured as follows:
- Set stars fill color in normal state with
app:srb_fillColor
/ setFillColor(@ColorInt int)
- Set stars background color in normal state with
app:srb_starBackgroundColor
/ setStarBackgroundColor(@ColorInt
int)
- Set stars fill color in pressed state with
app:srb_pressedFillColor
/ setPressedFillColor(@ColorInt int)
- Set stars background color in pressed state with
app:srb_pressedStarBackgroundColor
/
setPressedStarBackgroundColor(@ColorInt int)
有关更多配置选项,请单击 here。
RatingBar
中的星星只是 API <21 的 LayerDrawable
. The key to setting the color of unfilled stars is to get a handle on the index within the LayerDrawable
that contains a description of the drawable for unfilled stars (index = 0). Once we have that drawable, we can color it with "Drawable#setTint()" for API 21+ or with "Drawable#setColorFilter()"。
以下示例将未填充的星星设置为不透明的绿色:
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);
}
}
}
我正在尝试将 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?
未填充的颜色仍然是透明的,视图的背景是半白的!
您可以通过创建自定义可绘制资源文件来实现此目的,
在 drawable 目录中创建名称为 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"
试试这个布局来评星级
compile '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);
}
}
});
我认为 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" />
结果:
The view can be configured as follows:
- Set stars fill color in normal state with
app:srb_fillColor
/setFillColor(@ColorInt int)
- Set stars background color in normal state with
app:srb_starBackgroundColor
/setStarBackgroundColor(@ColorInt int)
- Set stars fill color in pressed state with
app:srb_pressedFillColor
/setPressedFillColor(@ColorInt int)
- Set stars background color in pressed state with
app:srb_pressedStarBackgroundColor
/setPressedStarBackgroundColor(@ColorInt int)
有关更多配置选项,请单击 here。
RatingBar
中的星星只是 API <21 的 LayerDrawable
. The key to setting the color of unfilled stars is to get a handle on the index within the LayerDrawable
that contains a description of the drawable for unfilled stars (index = 0). Once we have that drawable, we can color it with "Drawable#setTint()" for API 21+ or with "Drawable#setColorFilter()"。
以下示例将未填充的星星设置为不透明的绿色:
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);
}
}
}