Android的屏幕尺寸和屏幕密度图像选择 [英] Android Screen-size and Screen-density Image Selection

查看:343
本文介绍了Android的屏幕尺寸和屏幕密度图像选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用不同大小的设备水平的多个图像时有问题。我有7个独立的Ratingbars,每次使用不同的自定义图像。我攀登的所有图片,把它们纳入各自的DPI可绘制文件夹。我遇到的问题是,XLARGE MDPI屏幕(左上下面的图片中),他们配合得非常完美,但是当我在一个小屏幕观看他们,整个7 ratingbars太宽,以适应的范围之内该装置的,如下所示:

I am having problems when trying to use multiple horizontal images on different size devices. I have 7 separate Ratingbars, each using a different custom image. I have scaled all the images and placed them into their respective dpi drawable folders. The problem I'm having is that on the xlarge mdpi screen (top left of the image below), they fit perfectly, but when I'm viewing them on a smaller screens, the entire 7 ratingbars are too wide to fit within the bounds of the device, as shown below:

我按照对于图像中的正确缩放(基于MDPI的原始碱尺寸)如以下回答说:<一href=\"http://stackoverflow.com/a/11581786/1634369\">http://stackoverflow.com/a/11581786/1634369

I have followed the correct scaling for the images (based on the original base size of the mdpi) as stated in the following answer: http://stackoverflow.com/a/11581786/1634369

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

基于以上的比例

,以下是我所有的绘制文件夹,显示出它们各自的dpi的文件夹(所有基于该MDPI比)中的每个图像的尺寸:

Based on the scaling above, the following are all my drawable folders, showing the dimensions of each of the images within their respective dpi folders (all based off the mdpi ratio):

然后,为了显示每个Ratingbars,我设置了将使用默认的灰度图像,并根据评价值的彩色图像中的每个等级栏中选择。下图显示了code我用来显示每个使用布局Ratingbars的。注:我已根据来自基地MDPI值的宽度和高度

Then in order to display each of the Ratingbars, I set up a selector for each rating bar which will use a default grey image and a coloured image based on the rating value. The following shows the code I use to display each of the Ratingbars using the layout. NOTE: I have based the width and height from the base mdpi values.

<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:numStars="1"
        android:rating="1"
        android:stepSize="0.01"
        android:isIndicator="true"
        android:layout_width="75dp"
        android:layout_height="77dp"
        android:layout_gravity="bottom"
        android:gravity="bottom"                                                          
        android:progressDrawable="@drawable/selector_a" />

    <RatingBar            
        android:numStars="1"
        android:rating="0.5"
        android:stepSize="0.01"
        android:isIndicator="true"
        android:layout_width="73dp"
        android:layout_height="114dp"
        android:layout_gravity="bottom"
        android:gravity="bottom"                                                          
        android:progressDrawable="@drawable/selector_b" />

    <RatingBar            
        android:numStars="1"
        android:rating="0.5"
        android:stepSize="0.01"
        android:isIndicator="true"
        android:layout_width="63dp"
        android:layout_height="132dp"
        android:layout_gravity="bottom"
        android:gravity="bottom"                                                          
        android:progressDrawable="@drawable/selector_c" />

    <RatingBar            
        android:numStars="1"
        android:rating="0.5"
        android:stepSize="0.01"
        android:isIndicator="true"
        android:layout_width="79dp"
        android:layout_height="145dp"
        android:layout_gravity="bottom"
        android:gravity="bottom"                                                          
        android:progressDrawable="@drawable/selector_d" />

    <RatingBar            
        android:numStars="1"
        android:rating="0.5"
        android:stepSize="0.01"
        android:isIndicator="true"
        android:layout_width="79dp"
        android:layout_height="161dp"
        android:layout_gravity="bottom"
        android:gravity="bottom"                                                          
        android:progressDrawable="@drawable/selector_e" />

    <RatingBar            
        android:numStars="1"
        android:rating="0.5"
        android:stepSize="0.01"
        android:isIndicator="true"
        android:layout_width="108dp"
        android:layout_height="168dp"
        android:layout_gravity="bottom"
        android:gravity="bottom"                                                          
        android:progressDrawable="@drawable/selector_f" />

    <RatingBar            
        android:numStars="1"
        android:rating="0.5"
        android:stepSize="0.01"
        android:isIndicator="true"
        android:layout_width="147dp"
        android:layout_height="189dp"
        android:layout_gravity="bottom"
        android:gravity="bottom"                                                          
        android:progressDrawable="@drawable/selector_g" />

</LinearLayout>

会有人能够帮助我保证,无论什么筛选Ratingbars显示上,无论该设备是什么样的密度,它总是看起来一样(它看起来这是显示顶部的10.1 MDPI屏幕上的方式离开显示所有不同的屏幕图像的?任何帮助将大大AP preciated。

Would anyone be able to help me ensure that no matter what screen the Ratingbars are displayed on and no matter what density the device is, it will always look the same (the way it looks on the 10.1 mdpi screen which is displayed top left of the image showing all the different screens? Any help would be greatly appreciated.

非常感谢率先通过这个问题来读取时间。我放置了code Github上在以下位置: https://github.com/gbayo1/ RatingBarScalingIssue.git

Thanks very much for taking the time to read through this question. I have placed the code on Github at the following location: https://github.com/gbayo1/RatingBarScalingIssue.git

推荐答案

您可以平均分配查看在屏幕上通过使用的LinearLayout layout_weight 。对于 layout_weight 来工作,你必须设置 layout_width 0dp 。这是正常的,不要让自己被那所迷惑。如果你想同样垂直分布您必须设置 layout_height 0dp 。在 layout_weight 通知的LinearLayout 多大的查看应相比于其他的。根据你的情况,我们希望所有的查看来平均分配,使他们都需要有相同的宽度。这就是为什么我们将所有查看的原因相同 layout_weight - 1 。你可以做更多与 layout_weight ,例如,如果您有两个视图和你想要的左视图占用屏幕的三分之二,是正确的占用屏幕的三分之一,你会分配左边的2 layout_weight ,以及一个 layout_weight 1的权利

You can equally distribute Views across the Screen by using a LinearLayout and layout_weight. For layout_weight to work you have to set layout_width to 0dp. That is normal don't let yourself be confused by that. If you wanted to equally distribute vertically you would have to set layout_height to 0dp. The layout_weight tells the LinearLayout how big a View should be compared to the other ones. In your case we want all Views to be equally distributed so they all need to have the same width. That's the reason why we assign all Views the same layout_weight - 1. You can do a lot more with layout_weight, for example if you had two Views and you wanted the left view to take up two thirds of the screen and the right one to take up one third of the screen you would assign the left one a layout_weight of 2, and the right one a layout_weight of 1.

我已经相应地修改您的布局:

I have modified your layout accordingly:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">


    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true">

        <RatingBar
                android:numStars="1"
                android:rating="1"
                android:stepSize="0.01"
                android:isIndicator="true"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:progressDrawable="@drawable/selector_a"/>

        <RatingBar
                android:numStars="1"
                android:rating="0.5"
                android:stepSize="0.01"
                android:isIndicator="true"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:progressDrawable="@drawable/selector_b"/>

        <RatingBar
                android:numStars="1"
                android:rating="0.5"
                android:stepSize="0.01"
                android:isIndicator="true"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:progressDrawable="@drawable/selector_c"/>

        <RatingBar
                android:numStars="1"
                android:rating="0.5"
                android:stepSize="0.01"
                android:isIndicator="true"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:progressDrawable="@drawable/selector_d"/>

        <RatingBar
                android:numStars="1"
                android:rating="0.5"
                android:stepSize="0.01"
                android:isIndicator="true"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:progressDrawable="@drawable/selector_e"/>

        <RatingBar
                android:numStars="1"
                android:rating="0.5"
                android:stepSize="0.01"
                android:isIndicator="true"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:progressDrawable="@drawable/selector_f"/>

        <RatingBar
                android:numStars="1"
                android:rating="0.5"
                android:stepSize="0.01"
                android:isIndicator="true"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:progressDrawable="@drawable/selector_g"/>

    </LinearLayout>
</RelativeLayout>

我假设有最终将除了 Ratingbars ,这就是为什么我已经包裹在一个一切 RelativeLayout的。因为您的自定义 progressDrawable ,因为我改变了 layout_height 来,你可能有一些问题与评级条的高度 WRAP_CONTENT 。如果它不工作,或者你希望他们有不同的高度,你可以设置一个固定的高度为 RatingBars ,但离开 layout_width ,因为它是。

I assume there will eventually be something else in the layout besides the Ratingbars, that's why I already wrapped everything in a RelativeLayout. Because of your custom progressDrawable you might have a few issues with the height of the rating bars since I changed layout_height to wrap_content. If it doesn't work or you want them to have different heights you can set a fixed height for the RatingBars but leave the layout_width as it is.

在一个侧面说明:在您的原始布局了所有这些 layout_gravity 重力元素基本上是无用的。要对齐所有的 RatingBars 来,你只会有需要的 Android的底部:重力=底的LinearLayout 本身。

On a side note: All those layout_gravity and gravity elements you had in your original layout were essentially useless. To align all the RatingBars to the bottom you would only have needed android:gravity="bottom" on the LinearLayout itself.

这篇关于Android的屏幕尺寸和屏幕密度图像选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆