【问题标题】:cut off image in ratingbar在评分栏中截断图像
【发布时间】:2019-01-04 15:21:32
【问题描述】:

我制作了自己的评分栏。鲜花图片有 4 种尺寸(xdpi、hdpi 等),从 24pxx24px 到 64x64px。

<?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/ic_rating_flowers_empty" />
    <item android:id="@+android:id/secondaryProgress"
          android:drawable="@drawable/ic_rating_flowers_empty" />
    <item android:id="@+android:id/progress"
          android:drawable="@drawable/ic_rating_flowers" />
</layer-list>

drawable flder 中的rating_flowers.xml

<style name="flowersRating" parent="@android:style/Widget.RatingBar">
    <item name="android:progressDrawable">@drawable/rating_flowers</item>
    <item name="android:minHeight">24dip</item>
    <item name="android:maxHeight">64dip</item>
</style>

在styles.xml中

<TableRow
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dip" >

    <TextView
        android:text="@string/mood_headache" />

    <RatingBar
        android:id="@+id/moodHeadacheRating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"    
        android:numStars="3"
        android:stepSize="1.0"
        android:rating="0" 
        style="@style/flowersRating" />

 </TableRow> 

和活动

我所看到的

以及来自 drawable-ldpi 的图像

【问题讨论】:

    标签: android


    【解决方案1】:

    尝试删除

    android:padding="5dip"
    

    来自 TableRow,因为它会在可绘制的花朵上方产生填充。

    【讨论】:

      【解决方案2】:

      在您的评分栏更改的 XML 中

      android:layout_height="wrap_content"
      

      android:layout_height="[some number]sp".
      

      这将解决图像被截断的问题。

      DisplayMetrics 可用于获取有关显示器的一般信息,然后确定要使用的适当单位,但使用 an android unit converter 会简单得多。

      【讨论】:

        【解决方案3】:

        使用样式作为 ?android:attr/ratingBarStyleSmall

        <RatingBar
        android:id="@+id/rating"
        style="?android:attr/ratingBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="5"
        android:progressDrawable="@drawable/ratingbar_selector"
        android:rating="2.0"
        android:stepSize="1.0" />
        

        自定义评分栏选择器将是: ratingbar_selector.xml

        <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
            <item
                android:id="@android:id/background"
                android:drawable="@drawable/ic_unselected_star" />
            <item
                android:id="@android:id/secondaryProgress"
                android:drawable="@drawable/ic_unselected_star" />
            <item
                android:id="@android:id/progress"
                android:drawable="@drawable/ic_selected_star" />
        </layer-list>
        

        IMP 注意:如果自定义评分栏图标大小为 48x48、50x50 等,则将图标放入 xxhdpi 文件夹(因为我使用了较小的(36x36)评分图标,这会产生同样的问题)。 然后我使用了大小为 48x48 的评分栏图标。

        【讨论】:

          【解决方案4】:

          我在这里遇到了完全相同的问题。对我来说,问题不在于代码,而在于图像本身。就我而言,解决方案是将照片手动调整为我想要的尺寸(IE 24x24 或 64x64)并将它们放入正确的文件夹中。

          我尝试了很多东西,但似乎

          <item name="android:minHeight">24dip</item>
          <item name="android:maxHeight">64dip</item>
          

          对我来说,一点代码不会做任何事情。尝试自己调整图像大小,看看是否能解决问题。

          -Sil

          【讨论】:

            【解决方案5】:

            设置android:layout_height="0dp"

            https://stackoverflow.com/a/44992485/4238544

            【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-02-02
            • 1970-01-01
            • 1970-01-01
            • 2018-10-04
            • 1970-01-01
            • 2012-06-27
            相关资源
            最近更新 更多