【问题标题】:Android RatingBar Screen Scaling IssueAndroid RatingBar屏幕缩放问题
【发布时间】:2014-05-02 21:49:37
【问题描述】:

我想知道是否有人可以帮助我。在不同的屏幕密度上显示时,我在使用自定义 RatingBar 时遇到了缩放问题。我已经建立了一个快速示例项目来解释我的问题。这只是一个非常基本的项目,目前只关注大屏幕尺寸,无论密度如何。

正如您从图像中看到的那样,我有用于 RatingBar 的选择器,然后我在每个大密度可绘制文件夹中为两个单独的 RatingBars 有一个默认和完整图像(目前所有图像大小相同)。为了显示这个自定义 RatingBar。我试图让这些 RatingBars 正确显示,并尝试了两种不同的方法,如下所述。第一种方式我尝试在布局文件中指定RatingBars的宽高,如下图:

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

    <RatingBar 
        android:id="@+id/rb_test_1"           
        android:numStars="1"
        android:rating="0.5"
        android:stepSize="0.01"
        android:isIndicator="true"
        android:layout_width="150dp"
        android:layout_height="111dp"
        android:layout_gravity="bottom"
        android:gravity="bottom"                                                          
        android:progressDrawable="@drawable/selector_rb_test_1" />

    <RatingBar 
        android:id="@+id/rb_test_2"           
        android:numStars="1"
        android:rating="0.5"
        android:stepSize="0.01"
        android:isIndicator="true"
        android:layout_width="200dp"
        android:layout_height="154dp"
        android:layout_gravity="bottom"
        android:gravity="bottom"                                                          
        android:progressDrawable="@drawable/selector_rb_test_2" />

</LinearLayout>

查看 GUI 图形布局时,会在基本 10.1 屏幕尺寸与 Nexus 10 上产生以下输出。它似乎在复制和拉伸图像。

现在,当我尝试第二种方式时,这更像是我想要的方式(不必指定每个 RatingBars 的确切大小,只需将布局宽度和高度设置为换行,以下是代码:

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

    <RatingBar 
        android:id="@+id/rb_test_1"           
        android:numStars="1"
        android:rating="0.5"
        android:stepSize="0.01"
        android:isIndicator="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:gravity="bottom"                                                          
        android:progressDrawable="@drawable/selector_rb_test_1" />

    <RatingBar 
        android:id="@+id/rb_test_2"           
        android:numStars="1"
        android:rating="0.5"
        android:stepSize="0.01"
        android:isIndicator="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:gravity="bottom"                                                          
        android:progressDrawable="@drawable/selector_rb_test_2" />

</LinearLayout>

查看 GUI 图形布局时,会在基本 10.1 屏幕尺寸与 Nexus 10 上产生以下输出。这会切断 RatingBar 的图像底部。

无论屏幕的密度如何,我都希望能够毫无问题地显示多个 RatingBar。谁能帮我弄清楚为什么它在 Nexus10 屏幕上显示不同以及如何修复它?我已经坚持了一段时间了,如果有人能够阐明这一点,我将非常感激。非常感谢您。

【问题讨论】:

  • 首先引起我注意的是您将 minWidth 设置为 150dp:android:minWidth="150dp",但将 maxWidth 设置为仅 111dp:android:maxWidth="111dp"。身高也一样。为什么不将宽度和高度设置为固定大小?像这样:android:layout_width="150dp" android:layout_height="111dp".
  • 非常感谢您的评论。我会在早上检查这第一件事并让您知道,并按照您的建议交换设置布局宽度和高度。我希望上面的 min 和 max 只是一个错字,我已将 min 和 max height 指定为 111, min 和 max width 指定为 150。我会首先检查。非常感谢您抽出宝贵时间发表评论。
  • 我的意思是在您的资源中,您应该在 4 个不同的文件夹中有 4 个不同版本的图像。这些版本中的每一个都有不同的分辨率,android 将为您的设备的像素密度选择正确的图像。如果这 4 张图片之一的分辨率不正确,则该图片在某些设备上的尺寸会有所不同。
  • 你需要的文件夹是drawable-mdpidrawable-hdpidrawable-xhdpidrawable-xxhdpi。您可以找到更多信息here
  • 只需使用Android Asset Studio 或图像缩放器来缩放您的图像,然后检查您的问题是否已解决,生成的图像可能太小,但如果这解决了您的问题,我们知道是问题所在。之后,我可以帮助您将图像调整到正确的尺寸。

标签: android scaling screen-size ratingbar


【解决方案1】:

按照上面的@Xavers cmets,这个问题是由于图像的缩放不正确。解决方案是遵循比例,如以下答案所示 - https://stackoverflow.com/a/11581786/1634369

ldpi | mdpi | tvdpi | hdpi | xhdpi | xxhdpi | xxxhdpi
0.75 | 1    | 1.33  | 1.5  | 2     | 3      | 4

感谢您的帮助@Xaver

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 1970-01-01
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多