带有自定义可绘制对象的RatingBar工作异常

时间:2018-12-13 08:41:51

标签: android ratingbar

我想对我自己的两个图像使用RatingBar。 我正在寻找解决方案,但仍然失败。 这里有一个最佳答案的主题: https://stackoverflow.com/a/5800686/7329859

我决定创建一个新项目,以仅实现RatingBar的功能,例如上一主题中的解决方案。项目在这里:https://github.com/Wiktorl4z/ratingbar

ratingbar.xml

    <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/background"
        android:drawable="@drawable/ratingbar_empty" />
    <item android:id="@+id/secondaryProgress"
        android:drawable="@drawable/ratingbar_empty" />
    <item android:id="@+id/progress"
        android:drawable="@drawable/ratingbar_filled" />
</layer-list>

ratingbar_empty.xml

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

<!-- This is the rating bar drawable that is used to
 show a filled cookie. -->
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">

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

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

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

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

</selector>

ratingbar_filled.xml

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

<!-- This is the rating bar drawable that is used to
show a unfilled cookie. -->
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">

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

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

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

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

</selector>

styles.xml

   <style name="CustomRatingBar" parent="Widget.AppCompat.RatingBar">
        <item name="android:progressDrawable">@drawable/ratingbar</item>
        <item name="android:minHeight">50dp</item>
        <item name="android:maxHeight">50dp</item>
    </style>

activity.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <RatingBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style= "@style/CustomRatingBar"
        android:id="@+id/ratingBar"
        android:numStars="9" />

</LinearLayout>

This is how it display

花了几个小时后,我决定问一下stackoverflow,我做错了什么?

2 个答案:

答案 0 :(得分:0)

尝试使用png图像。 RatingBar不适用于svg。

答案 1 :(得分:0)

所以大事发生在ratingbar.xml中 您必须全部更改

Flux.flatMap

class App extends React.Component { state = { title: "Hello World", companies: [ "Google", "Apple", "Facebook" ], }; updateCompany(newName, index) { const { companies } = this.state; const newCompanies = [...companies]; newCompanies[index] = newName; this.setState({ companies: newCompanies }); } render() { const { companies } = this.state; return ( <ul> {companies.map((item, index) => ( <input type="text" key={index} value={item} onChange={(e) => this.updateCompany(e.target.value, index)}></input> ))} </ul> ); } }

此更改后,一切正常。