对于低于 23 的 API,图层列表中的可绘制形状错误呈现 [英] Shape drawable in layer-list rendered incorrectly for APIs below 23

查看:27
本文介绍了对于低于 23 的 API,图层列表中的可绘制形状错误呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天,我已切换到 Android Studio 2.1.3 版.今天,我重新打开了我的一个项目,并尝试添加一些图层列表可绘制对象以与 ImageView 一起使用.例如这个:

Yesterday, I've switched to Android Studio version 2.1.3. Today, I've re-opened one of my projects and tried to add some layer-list drawables for use with an ImageView. For example this one:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:width="80px" android:height="80px"
        android:top="0px" android:left="0px" 
        android:right="0px" android:bottom="0px">
        <shape android:shape="rectangle">
            <solid android:color="@color/deep_orange"/>
        </shape>
    </item>
    <item android:width="60px" android:height="60px"
        android:top="10px" android:left="10px" 
        android:right="10px" android:bottom="10px">
        <shape android:shape="oval">
            <solid android:color="@color/light_yellow"/>
        </shape>
    </item>
</layer-list>

这是我在升级我的 Android Studio 版本之前得到的,如果我选择 23 作为在 IDE 中渲染布局时使用的 Android 版本",这就是我现在在预览中得到的.

This is what I used to get before upgrading my Android Studio version, and this is what I get now in the Preview if I choose 23 as "Android version to use when rendering layouts in the IDE".

但是,如果我选择低于 23 的任何版本,我只会得到正方形:

However, if I choose any version below 23, I get just the square:

不幸的是,在模拟器 (Lollipop 22) 或设备 (Jellybean 18) 上运行应用程序时,我也只得到正方形.

Unfortunately, I also get nothing but the square when running the app on an emulator (Lollipop 22) or a device (Jellybean 18).

以防万一这是有帮助的:

Just in case this is helpful:

  • compileSdkVersion 23
  • buildToolsVersion "23.0.3"
  • minSdk 版本 11
  • targetSdkVersion 23
  • 没有什么可用于 buildType 调试
  • 依赖项:编译'com.android.support:appcompat-v7:23.0.1'
  • Activity 扩展了 AppCompatActivity

我认为这个问题在某种程度上与 topleftrightbottom 的使用有关上层.因为如果我只是在一个正方形的顶部放置一个圆圈,预览以及真实的东西"在所有 API 级别都能按预期工作.

I think the problem is somehow related to the use of top, left, right and bottom for the upper layer. Because if I simply place a circle on top of a square, the preview as well as the "real thing" work as expected for all API levels.

EDIT 我还使用相同的代码/xml 和以下(自动创建的)build.gradle 开始了一个全新的项目:

EDIT I also started a brand new project with the same code/ xml and the following (automatically created) build.gradle:

  • compileSdkVersion 24
  • buildToolsVersion "24.0.2"
  • minSdk 版本 15
  • targetSdkVersion 24
  • 没有什么可用于 buildType 调试
  • 依赖项:编译'com.android.support:appcompat-v7:24.2.0'
  • Activity 扩展了 AppCompatActivity

这里的行为相同:如果我使用 topleftright,则不会为 API 级别 <= 22 呈现上层和 bottom 值 != 0.

The same behaviour here: the upper layer is not rendered for API leves <= 22 if I use top, left, right and bottom with values != 0.

所以我的问题是:如何使用 Android Studio 2.1.3 为所有 API 级别创建带有插图"的形状可绘制对象?

So my question is: how can I create shape drawables "with insets" for all API levels using Android Studio 2.1.3?

推荐答案

首先感谢@pskink 帮我分析问题:)

First of all, I'd like to thank @pskink for helping me to analyse the problem :)

问题中的 xml 适用于 API 级别 23+,因为

The xml in the question worked for API level 23+, because

可以在item"标签内设置宽度和高度

it is possible to set width and height right within the "item" tag

like @android developer 状态在 此 SO 帖子

它不适用于较低版本,因为需要使用 ShapeDrawable 设置 widthheight 属性尺寸 标签.

It did not work for lower versions, because there one needs to set width and height attributes for a ShapeDrawable using the size tag.

我想 Android 需要大小来确定如何缩放可绘制对象(-> 纵横比!),同时考虑到插图(顶部、左侧、右侧、底部).

I suppose Android needs the size to figure out how to scale the drawable (-> aspect ratio!) while taking into account the insets (top, left, right, bottom).

有时会出现格式错误(对于特定 API 级别)xml 的情况,Android 会默默地失败,只是没有绘制形状.

And as is sometimes the case with malformed (for a specific API level) xml, Android failed silently and simply did not draw the shape.

所以解决方案是:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:width="80px" android:height="80px"
        android:top="0px" android:left="0px" 
        android:right="0px" android:bottom="0px">
        <shape android:shape="rectangle">
            <solid android:color="@color/deep_orange"/>
        </shape>
    </item>
    <item android:width="60px" android:height="60px"
        android:top="10px" android:left="10px" 
        android:right="10px" android:bottom="10px">
        <shape android:shape="oval">
            <solid android:color="@color/light_yellow"/>
            <size android:height="60px" android:width="60px"/>
        </shape>
    </item>
</layer-list>

这篇关于对于低于 23 的 API,图层列表中的可绘制形状错误呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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