Android RatingBar - 一个完整的混乱

时间:2012-09-17 07:15:56

标签: android android-layout android-ui

我知道这听起来很不错,但有时候感觉就像Android的UI组件和行为有些不合时宜。

考虑以下XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

    <RatingBar
    android:id="@+id/ratingBar1"
    android:style="@android:style/Widget.Holo.Light.RatingBar.Small"
    android:progress="3"
    android:max="5"
    android:numStars = "5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />


</LinearLayout>

屏幕截图显示不同设备尺寸的显示效果:

Nexus S rating bar Nexus S评级栏

Tablet rating bar 平板电脑评级栏

Small Device rating bar 小型设备评级栏

即使它仅在预览工具中,我也可以确认它实际上是在设备上看起来的样子。

小型设备的行为令人抓狂。指定5颗星的RatingBar如何才能显示3,为什么不缩小星星以适应窗口?

认为wrap_content宽度是问题,我切换到layout_width =“fill_parent”并且没有改变小型设备上的外观,但它完全搞砸了Tablets(对于“numStars”为5)这么多:

Tablet with fill_parent for width

我的问题是,在尺寸方面,有没有办法让RatingBar获得正确的行为?您会认为numStars或max足够,但它完全是任意的。当RatingBar被拉伸以便它绘制更多的星星时,它会添加星星并看到如何以3的进度,它根据显示的星数绘制它?没有遵守numStars就没有意义了!

如果无法从RatingBar获得逻辑性能,是否有任何替代方案,包括第三方小部件?如果没有,我想我总是可以绘制5个ImageViews,并对其进行相应的操作。

(请记住,布局中只有一个RatingBar会显示所有这些行为 - 忘记尝试使用其他小部件/组件调整大小,或者使用自己的样式。我知道它已经完成,但为什么这是默认行为? )

1 个答案:

答案 0 :(得分:2)

在使用股票评级栏时,我的感觉和你一样,所以我自己制作了SimpleRatingBar

它的特点:

  • 完全正常工作android:layout_width:可以将其设置为wrap_contentmatch_parent或abritary dp。
  • 任意数量的明星。
  • 任意步长。
  • 可以精确控制星星的大小,也可以通过设置最大大小来控制。
  • 可自定义的颜色(星星的边框,填充和背景)。
  • 明星之间可定制的尺寸分离。
  • 可自定义的星星边框宽度。
  • 允许设置OnRatingBarChangeListener
  • 星星填充可以设置为从左到右或从右到左(RTL语言支持)。
  • 在视图中集成了AnimationBuilder,以动画方式设置评级。

Here is a preview of it

您可以在jcenterMaven Central中找到它。因此,在build.gradle文件中,只需添加依赖项:

compile 'com.iarcuschin:simpleratingbar:0.1.+'

我希望它有用。