【问题标题】:Problem in reducing size of Rating Bar.减小评级栏大小的问题。
【发布时间】:2011-10-16 11:08:27
【问题描述】:

我想减小评级栏的大小,我有一些样式属性可以做到这一点,但它们超出了用户交互的范围,它们只是指示器。所以,请让我知道如何减小尺寸。提前致谢。

【问题讨论】:

标签: android android-widget rating


【解决方案1】:

如何粘贴here 给出的代码...

第 1 步。您需要在res/drawable 中拥有自己的评分星...

步骤 2 在res/drawable 你需要ratingstars.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_empty" />
    <item android:id="@+android:id/secondaryProgress"
          android:drawable="@drawable/star_empty" />
    <item android:id="@+android:id/progress"
          android:drawable="@drawable/star" />
</layer-list>

第 3 步在res/values 你需要styles.xml 如下...

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="foodRatingBar" parent="@android:style/Widget.RatingBar">
        <item name="android:progressDrawable">@drawable/ratingstars</item>
        <item name="android:minHeight">22dip</item>
        <item name="android:maxHeight">22dip</item>
    </style>
</resources>

第 4 步在您的布局中...

<RatingBar 
      android:id="@+id/rtbProductRating"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"
      android:numStars="5"
      android:rating="3.5"
      android:isIndicator="false"
      style="@style/foodRatingBar"    
/>  

尝试活动 ....

package x.y;

import android.app.Activity;
import android.os.Bundle;

public class RatingBarDemo extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.product);

    }
}

有了这个布局product.xml ...

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="5dip"
    android:paddingBottom="5dip">
    <ImageView
        android:id="@+id/imgProduct"
        android:layout_width="50dip"
        android:layout_height="50dip" 
        android:src="@drawable/icon" 
        android:scaleType="centerCrop"
        />
    <RelativeLayout
        android:id="@+id/layProductInfo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/imgProduct"
        android:paddingLeft="5dip"
        android:paddingRight="0dip"
        android:paddingTop="5dip"
        android:paddingBottom="5dip">  
        <TextView
            android:id="@+id/tvProductName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Product Name"
            android:textSize="17dip" 
            />
        <RatingBar 
            android:id="@+id/rtbProductRating"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:numStars="5"
            android:rating="3.5"
            android:isIndicator="false"
            style="@style/foodRatingBar"
            android:layout_below="@id/tvProductName"
            />  
        <TextView
            android:id="@+id/tvPriceLabel"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="$45.87"
            android:layout_toRightOf="@id/rtbProductRating"
            android:layout_below="@id/tvProductName"
            android:paddingLeft="10dip"
            android:textSize="17dip" 
            />  
      </RelativeLayout>     
  </RelativeLayout>

【讨论】:

  • 你实际上不需要半星图像。
  • 如果我以这种方式使用评级栏,那么评级明星图像将在下方拉伸。我能做些什么来防止这种情况发生?
  • AFAIK 有平台限制,评分星不会自动缩放,所以评分星的drawable大小必须与maxHeight和maxWidth相同。
【解决方案2】:

我在这里搜索了很多具有相同问题的帖子,我正在尝试@Vaibhav A. Jani 提供的答案,当时我只是通过一个更简单的解决方案来解决这个问题。我并不是说@Vaibhav A. Jani 的答案不正确。

试试这个,它让我看到小得多的星星:

RatingBar ratingBar = new RatingBar(context, null, android.R.attr.ratingBarStyleSmall);

而且我还能够动态创建大量 RatingBar,而无需接触任何 xml 文件,只需 java 编码。

Reference

【讨论】:

    【解决方案3】:

    尝试改变风格

     <RatingBar
            android:layout_width="wrap_content"
            android:rating="4.5"
            style="@style/Base.Widget.AppCompat.RatingBar.Small"
            android:layout_height="wrap_content" />
    

    【讨论】:

      【解决方案4】:

      工作代码: 您可以轻松减小评分栏星的大小 只需添加xml如下

      android:scaleX="0.5" android:scaleY="0.5"

        <RatingBar
                  android:id="@+id/ratingBar_visibility"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:numStars="5"
                  android:scaleX="0.5"
                  android:scaleY="0.5" />
      

      【讨论】:

        猜你喜欢
        • 2015-12-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-20
        • 2011-09-14
        • 1970-01-01
        • 2013-10-12
        相关资源
        最近更新 更多