【问题标题】:where to put drawable image for specific screen size在哪里放置特定屏幕尺寸的可绘制图像
【发布时间】:2017-06-20 05:43:40
【问题描述】:

我有一个像这样的图像视图

 <ImageView 
 android:layout_width = "wrap_content"
 android:layout_height="wrap_content"
 android:src = "@drawable/myimage"/>

图像文件“myimage.png”位于所有 4 个不同密度的可绘制文件夹中。手机上的景色看起来不错。如果我在平板电脑上显示它(比如 10 英寸),那么图像“看起来”太小了,因为空间太大了。

我知道我可以为大屏幕尺寸创建布局,但是我应该在哪里放置更大尺寸的图像文件?这样不仅可以根据密度选择图像文件,还可以根据屏幕尺寸选择图像文件

谢谢

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    您应该使用sw 表示法。

    例如:

    layout/activity_with_photo.xml
    layout-sw600dp/activity_with_photo.xml // that's a 7 inch tablet
    layout-sw720dp/activity_with_photo.xml // that's a nexus 9 and up
    

    然后是这些布局的更大图像

    // here a 7 inch tabled with all the densities
    layout-mdpi-sw600dp/photo.png
    layout-hdpi-sw600dp/photo.png
    layout-xdpi-sw600dp/photo.png
    layout-xxdpi-sw600dp/photo.png
    
    // here a nexus 9 n up with all densities
    layout-mdpi-sw720dp/photo.png
    layout-hdpi-sw720dp/photo.png
    layout-xdpi-sw270dp/photo.png
    layout-xxdpi-sw720dp/photo.png
    

    或者,如果您在运行时对这些资源进行适当的缩放,您可以添加到 no-dpi 文件夹

    layout-nodpi-sw600dp/photo.png
    layout-nodpi-sw270dp/photo.png
    

    但是,如果您只使用一个,请在此处注意这一点:Bitmap too large to be uploaded into a texture

    【讨论】:

      【解决方案2】:

      简单步骤:-

      1. 如果您使用的是股票图标。去这个网站https://material.io/icons/。选择并下载大小为 48dp 的图标。

      2. 解压 zip 文件并选择 android 下的所有文件夹并将它们复制到项目的 res 文件夹中。就是这样。

      【讨论】:

      • 我不是在问密度。我在询问屏幕尺寸
      • Go to res> values> 添加新文件命名“dimens”> 在限定符中选择“Size”,然后选择“screen size”从小到 x-large。然后在新的 (x-large) 尺寸文件下添加您的大屏幕尺寸。
      【解决方案3】:

      屏幕大小的特定资源由smallmediumlargexlarge 表示。所以你需要一个drawable-small 和/或layout-small 文件夹等。

      来源:https://developer.android.com/guide/practices/screens_support.html

      编辑:自 Android 3.2 起,屏幕大小的文件夹已被弃用。请使用密度文件夹

      【讨论】:

      • 哦,我不知道您也可以使用屏幕尺寸作为可绘制名称。因此,如果您在 drawable-small 和 drawable-xdpi 中有图像,那么在一个小的 xdpi 屏幕中,它从哪个文件夹获取图像?
      • 我刚刚注意到这些已被弃用。请使用密度文件夹
      【解决方案4】:

      一个棘手的方法是将 imageView 的 scaleType 设置为 fitcenter 并将恒定的宽度和高度设置为您的 imageview。它会将您的图标放大到指定的宽度和高度

      <ImageView
          android:layout_width="200dp"
          android:layout_height="200dp"
          android:scaleType="fitCenter"
          android:src="@drawable/my_photo"/>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-20
        • 1970-01-01
        • 2015-02-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多