【问题标题】:Android RatingBar below Lollipop renders incorrectlyLollipop 下面的 Android RatingBar 渲染不正确
【发布时间】:2015-06-10 10:27:25
【问题描述】:

我正在使用我的自定义评分栏。带有 5 颗星的顶部图像在 Lollipop 上,我的评分栏工作正常。然而,在 Lollipop(Kitat、JellyBean、ICS)版本下,评级栏被弄乱了(用单星检查第二张图片) 可能是什么问题?我尝试了所有可能的方法,更改主题,管理高度宽度......似乎没有任何工作:(

这就是风格

<style name="customRatingBar" parent="@android:style/Widget.RatingBar">

    <item name="android:progressDrawable">@drawable/rating_custom_bar</item>

</style>

这是评分栏 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/star_blank"/>
    <item
        android:id="@+android:id/secondaryProgress"
        android:drawable="@drawable/star_blank"/>
    <item
        android:id="@+android:id/progress"
        android:drawable="@drawable/star_filled"/>
</layer-list>

这是星空白

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

    <item android:drawable="@drawable/rating_empty" android:state_pressed="true" android:state_window_focused="true"/>
    <item android:drawable="@drawable/rating_empty" android:state_focused="true" android:state_window_focused="true"/>
    <item android:drawable="@drawable/rating_empty" android:state_selected="true" android:state_window_focused="true"/>
    <item android:drawable="@drawable/rating_empty"/>

</selector>

这是满天星

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

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

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

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

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

</selector>

【问题讨论】:

    标签: ratingbar


    【解决方案1】:

    我有同样的问题,花了很多时间,尝试在 ratingBar 设置所有参数但没有工作,但有些奇怪。我在一个项目中和一个朋友一起工作,他编译的代码与我完全相同,没有这个问题,所以我将 IDE 更改为他的,一切都开始工作了。

    我使用的是“Android 开发人员工具”,您曾经在 https://developer.android.com/training/index.html 下载的 Eclipse,它是一个已经嵌入 ADT 的 Eclipse。 我更改为 Java EE 并按照此说明下载 ADT http://developer.android.com/sdk/installing/installing-adt.html

    我希望这可以帮助您解决问题。

    【讨论】:

    • 我正在使用 Android Studio。在这种情况下,您有什么建议?
    • 我也通过 intellij idea 运行了我的项目。同样的问题。但是,我还没有尝试过eclipse。因为我的是一个 gradle 项目,我认为除非我通过手动处理库依赖项来修改我的项目以使用 eclipse,否则 eclipse 将无法如此轻松地运行。
    【解决方案2】:

    已经有一段时间了,但从来没有回答过。这是我的 3 步解决方案,无论 SDK 版本如何。

    1- 在您的 xml 布局中将此添加为您的 RatingBar:

    <RatingBar
    android:id="@+id/new_ratingbar"
    android:progressDrawable="@drawable/custom_drawable" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numStars="5"
    android:stepSize="0.5"/>
    

    如果需要,可以给 RatingBar 赋予 minHeight 和 maxHeight 或根据需要添加其他属性。

    2- 这是您将上述progressDrawable 引用到的drawable:

    custom_drawable.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/rating_bar_empty" /> <!--this is your .png file-->
        <item
            android:id="@android:id/secondaryProgress"
            android:drawable="@drawable/rating_bar_fill" /> <!--this is your .png file-->
        <item
            android:id="@android:id/progress"
            android:drawable="@drawable/rating_bar_fill" /> <!--this is your .png file-->
    </layer-list>
    

    3- 最后一件事是在您的各种可绘制文件夹中提供 .png 文件。

    【讨论】:

      猜你喜欢
      • 2011-03-19
      • 1970-01-01
      • 2016-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多