【问题标题】:Custom Rating Bar show one star only自定义评分栏仅显示一颗星
【发布时间】:2019-03-04 15:10:34
【问题描述】:

.

我创建了一个自定义评分栏。我设置了 5 个自定义星图,但它只显示了一个。无论设置的星数如何,我都只看到一个,而不是可变数量的评级可绘制对象。任何帮助将不胜感激。

xml.code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">

<RatingBar
    android:id="@+id/ratingbar_default"
    style="@style/RatingBar"
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_marginBottom="20dp" />

<TextView
    android:id="@+id/textView"
    android:layout_width="276dp"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="show state up checkbox"
    android:textColor="#CC00CC"
    android:textSize="20dp" />

java代码:

Activity RatingBarActivity

@Override
    protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_rating);

            final TextView text = (TextView) findViewById(R.id.textView);

            final RatingBar ratingBar_default = (RatingBar) findViewById(R.id.ratingbar_default);

            ratingBar_default.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener()
                {
                    @Override
                    public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser)
                        {
                            // TODO Auto-generated method stub
                            text.setText("Rating: " +         String.valueOf(rating));
                        }
                });

 <?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_ratingbar_full_empty" />
<item
    android:id="@android:id/secondaryProgress"
    android:drawable="@drawable/star_ratingbar_full_empty" />
<item
    android:id="@android:id/progress"
    android:drawable="@drawable/star_ratingbar_full_filled" />
</layer-list>

res/drawable/star_ratingbar_full_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/star2" />

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

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

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

</selector>

res/drawabstar_ratingbar_full_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/star3" />

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

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

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

</selector>

styles.xml 文件:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"></style>

<style name="RatingBar" parent="@android:style/Widget.RatingBar">
    <item     name="android:progressDrawable">@drawable/star_rating_bar_full</item>
    <item name="android:minHeight">48dip</item>
    <item name="android:maxHeight">48dip</item>
</style>

</resources>

【问题讨论】:

标签: android


【解决方案1】:

在我的情况下,问题在于图层列表中的 SVG 可绘制对象。当我将它们更改为 PNG 时,它开始正常工作

【讨论】:

  • 在我的情况下也是同样的问题。但是如果只想使用 svgs 怎么办?
  • @Pinkesh Darji 如果新的支持库仍然重现这种情况,唯一的办法就是使用老派剪辑
【解决方案2】:

尝试在布局文件activity_rating.xml 中为RatingBar 指定android:numStars 属性。我猜,默认值是 1 - 这就是为什么你只看到一颗星

【讨论】:

  • 酷软我已经试过这个
  • 您屏幕截图上的绿色星星似乎是来自层 @android:id/background 的 full_empty 星星。它对水龙头有反应吗?我还查看了@drawable/ratingbar_full_holo_dark,发现它不使用选择器。如果您使用静态图像而不是选择器,您的代码会显示所有星星吗? Holo 评分栏还重新定义了 indeterminateDrawable(以及 progressDrawable)。我想你也需要定义它。
  • 这根本不是这个问题的答案。我们已经尝试过 numStars=5
【解决方案3】:

只需从drawable中删除评级栏填充和空图像的any-dpidrawable。它会为你工作。因为它显示自定义评分栏,所以您可以在layer list 中同时提供xml 和 png 文件。但要显示所有星星,您只需将png 图标放入drawable 文件夹。

【讨论】:

    【解决方案4】:

    已经有一段时间了,但从来没有回答过。 这是我的 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- 这是您将上述 android:progressDrawable 引用到的可绘制对象:

    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 文件。

    【讨论】:

      猜你喜欢
      • 2021-02-06
      • 1970-01-01
      • 2016-09-23
      • 2015-05-23
      • 1970-01-01
      • 2023-01-03
      • 1970-01-01
      相关资源
      最近更新 更多