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

查看:131
本文介绍了对于低于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).

以防万一:

  • compileSdkVersion 23
  • buildToolsVersion"23.0.3"
  • minSdkVersion 11
  • targetSdkVersion 23
  • 什么都不能进行buildType调试
  • 依赖关系:编译'com.android.support:appcompat-v7:23.0.1'
  • Activity扩展AppCompatActivity
  • compileSdkVersion 23
  • buildToolsVersion "23.0.3"
  • minSdkVersion 11
  • targetSdkVersion 23
  • nothing for buildType debug
  • dependencies: compile 'com.android.support:appcompat-v7:23.0.1'
  • the Activity extends 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.

编辑,我还使用相同的代码/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"
  • minSdkVersion 15
  • targetSdkVersion 24
  • 什么都不能进行buildType调试
  • 依赖关系:编译'com.android.support:appcompat-v7:24.2.0'
  • Activity扩展AppCompatActivity
  • compileSdkVersion 24
  • buildToolsVersion "24.0.2"
  • minSdkVersion 15
  • targetSdkVersion 24
  • nothing for buildType debug
  • dependencies: compile 'com.android.support:appcompat-v7:24.2.0'
  • the Activity extends AppCompatActivity

此处的行为相同:如果我将topleftrightbottom的值设置为!= 0,则不会显示API级别< = 22的上层.

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

它不适用于较低版本,因为需要使用size标记为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天全站免登陆