我正在努力制作一个定制的评级栏。我试图制作一个定制的风格
<style
name="customRatingbar" parent="@android:style/Widget.RatingBar">
<item name="android:progressDrawable">@drawable/layerlist_ratingbar</item>
<item name="android:maxHeight">48dp</item>
<item name="android:minHeight">48dp</item>
</style>
layerlist_ratingbar是
<layer-list xmlns:android="http://schemas.android.com/apk/res /android">
<item android:id="@android:id/background"android:drawable="@drawable/rating_bar_empty" />
<item android:id="@android:id/secondaryProgress"android:drawable="@drawable/rating_bar_empty" />
<item android:id="@android:id/progress"android:drawable="@drawable/rating_bar_fill" />
</layer-list>
rating_bar_empty:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:state_focused="true"
android:state_window_focused="true"
android:drawable="@drawable/ic_rate_empty"></item>
<item
android:state_selected="true"
android:state_window_focused="true"
android:drawable="@drawable/ic_rate_empty"
></item>
<item
android:state_pressed="true"
android:state_window_focused="true"
android:drawable="@drawable/ic_rate_empty" ></item>
<item
android:drawable="@drawable/ic_rate_empty"></item>
</selector>
rating_bar_fill:
<selector xmlns:android="http://schemas.android.com/apk/re/android">
<item
android:state_focused="true"
android:state_window_focused="true"
android:drawable="@drawable/ic_rate_full"></item>
<item
android:state_selected="true"
android:state_window_focused="true"
android:drawable="@drawable/ic_rate_full"
></item>
<item
android:state_pressed="true"
android:state_window_focused="true"
android:drawable="@drawable/ic_rate_full" ></item>
<item
android:drawable="@drawable/ic_rate_full"></item>
我的控制台看起来很像
12-10 15:30:05.540: E/AndroidRuntime(17129): FATAL EXCEPTION: main
12-10 15:30:05.540: E/AndroidRuntime(17129): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.syntax.customratingbar/com.syntax.customratingbar.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class android.widget.RatingBar
12-10 15:30:05.540: E/AndroidRuntime(17129): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
12-10 15:30:05.540: E/AndroidRuntime(17129): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2377)
12-10 15:30:05.540: E/AndroidRuntime(17129): at android.app.ActivityThread.access$600(ActivityThread.java:167)
12-10 15:30:05.540: E/AndroidRuntime(17129): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
12-10 15:30:05.540: E/AndroidRuntime(17129): at android.os.Handler.dispatchMessage(Handler.java:99)
12-10 15:30:05.540: E/AndroidRuntime(17129): at android.os.Looper.loop(Looper.java:153)
12-10 15:30:05.540: E/AndroidRuntime(17129): at android.app.ActivityThread.main(ActivityThread.java:5341)
12-10 15:30:05.540: E/AndroidRuntime(17129): at java.lang.reflect.Method.invokeNative(Native Method)
12-10 15:30:05.540: E/AndroidRuntime(17129): at java.lang.reflect.Method.invoke(Method.java:511)
12-10 15:30:05.540: E/AndroidRuntime(17129): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:929)
12-10 15:30:05.540: E/AndroidRuntime(17129): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
12-10 15:30:05.540: E/AndroidRuntime(17129): at dalvik.system.NativeStart.main(Native Method)
12-10 15:30:05.540: E/AndroidRuntime(17129): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class android.widget.RatingBar
12-10 15:30:05.540: E/AndroidRuntime(17129): at android.view.LayoutInflater.createView(LayoutInflater.java:613)
12-10 15:30:05.540: E/AndroidRuntime(17129): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
12-10 15:30:05.540: E/AndroidRuntime(17129): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
12-10 15:30:05.540: E/AndroidRuntime(17129): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
12-10 15:30:05.540: E/AndroidRuntime(17129): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
activity_main xml
在xml中我只是改变我的评级栏的背景
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@style/customRatingbar"
android:numStars="4"
android:rating="3.0"
android:id="@+id/myRatingBar"
android:isIndicator="true"/>
答案 0 :(得分:2)
更改
android:background="@style/customRatingbar"
到
style="@style/CustomRatingBar"