简体   繁体   中英

Rating bar with custom drawable not showing half star only full stars

Hello I have a rating bar with custom drawable but it only displays full stars only and not showing half here is my code, how can I solve this?

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:id="@android:id/background"

    android:drawable="@drawable/empty_star" />
<item
    android:id="@+id/secondaryProgress"

    android:drawable="@drawable/empty_star" />

<item
    android:id="@android:id/progress"

    android:drawable="@drawable/filled_star" />
<style name="doctor_rating" parent="@style/Widget.AppCompat.RatingBar">
    <item name="android:minHeight">13dp</item>
    <item name="android:minWidth">65dp</item>
    <item name="android:numStars">5</item>
    <item name="android:progressDrawable">@drawable/custom_rating_stars</item>
    <item name="android:stepSize">.1</item>
</style>


     
                    <RatingBar
                    android:id="@+id/doctor_rating_bar"
                    style="@style/doctor_rating"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:layout_centerVertical="true"
                    android:layout_gravity="end"
                    android:rating="3.5"
                    />

Try this:

<style name="doctor_rating" parent="android.widget.RatingBar">
<item name="android:minHeight">13dp</item>
<item name="android:minWidth">65dp</item>
<item name="android:numStars">5</item>
<item name="android:progressDrawable">@drawable/custom_rating_stars</item>
<item name="android:stepSize">0.1</item>

See below code this will help you.

custom_rating_stars.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/ratingbar_empty" />
    <item
        android:id="@android:id/progress"
        android:drawable="@drawable/ratingbar_filled" />
</layer-list>

ratingbar_empty.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true"
        android:state_window_focused="true"
        android:drawable="@drawable/star_grey_small" />

    <item android:state_focused="true"
        android:state_window_focused="true"
        android:drawable="@drawable/star_grey_small" />

    <item android:state_selected="true"
        android:state_window_focused="true"
        android:drawable="@drawable/star_grey_small" />

    <item android:drawable="@drawable/star_grey_small" />

</selector>

ratingbar_filled.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true"
        android:state_window_focused="true"
        android:drawable="@drawable/star_yellow" />

    <item android:state_focused="true"
        android:state_window_focused="true"
        android:drawable="@drawable/star_yellow" />

    <item android:state_selected="true"
        android:state_window_focused="true"
        android:drawable="@drawable/star_yellow" />

    <item android:drawable="@drawable/star_yellow" />

</selector>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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