我的图层列表可绘制怎么了? [英] What is wrong with my layer-list drawable?

查看:114
本文介绍了我的图层列表可绘制怎么了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在此布局中将自定义可绘制对象设置为FloatingActionButton的android:src:

I wanted to set a custom drawable to be the android:src of FloatingActionButton in this layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:sscce="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        sscce:fabSize="normal"
        android:src="@drawable/fab_drawable" />

</LinearLayout>

我期待的是类似的东西

但是我得到了

这是自定义图层列表可绘制对象,其中包含一些形状可绘制对象:

Here is the custom layer-list drawable, which contains a few shape drawables:

res/drawable/fab_drawable.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape android:shape="oval">
            <solid android:color="#1AFF00" /> <!-- green -->
            <size android:width="150dp"
                android:height="150dp"/>
        </shape>
    </item>



    <item>
        <shape android:shape="oval">
            <solid android:color="#FC00FF" /> <!-- pink -->
            <size android:width="100dp"
                android:height="100dp"/>
        </shape>
    </item>



    <item>
        <shape android:shape="oval">
            <solid android:color="#0040FF" /> <!-- blue -->
            <size android:width="40dp"
                android:height="40dp" />
        </shape>
    </item>



    <item>
        <shape android:shape="oval" >
            <stroke android:width="3dp"
                android:color="#FF0000"/> <!-- red -->

            <solid android:color="#FFBF00" /> <!-- yellow -->
            <size android:width="24dp"
                android:height="24dp" />
        </shape>
    </item>

</layer-list>

推荐答案

尽管在语法上可以使用dp值,但这似乎会导致某些密度类别出现问题,因此最好使用px代替. 形状可绘制对象将按比例缩放以适合其包含的View,您提供的值只会影响形状的比例.

Although it is syntactically possible to use dp values, this seems to cause problems with some density categories, so better use px instead. Shape drawables will be scaled to fit their containing View anyway, the values you provide just have an influence on the proportions of the shape.

对于您的图层列表可绘制对象,您必须为所有形状可绘制对象的大小提供一个通用的框架",因为每一层将被缩放以独立于其他图层而适合View.

For your layer-list drawable, you have to provide a common "frame" for the size of all the shape drawables as each layer will be scaled to fit the View independently of the others.

这意味着较小的圆需要一些其他信息,有关它们应该从边缘绘制多远的信息.对于以其容器为中心的圆:

This means that the smaller circles need some additional information about how far from the edge they should be drawn. For a circle centered in its container:

距离=(container_size-circle_size)/2

如果我们以150px作为所有可绘制对象的基本尺寸,则例如粉红色的圆圈(尺寸为100px)需要与其容器边缘的距离为25px.

If we take 150px as the basic size for all the drawables, then for example the pink circle (size 100px) needs 25px distance to the edges of its container.

您可以通过为每个item设置属性来提供此信息:

You can provide this information by setting attributes to each item:

<item android:top="25px" android:left="25px" android:bottom="25px"  android:right="25px">
<shape android:shape="oval">
    <solid android:color="#FC00FF" /> <!-- pink -->
    <size android:width="100px"
          android:height="100px"/>
</shape>
</item>

请注意,重要的是,不仅要具有顶部/左侧的属性,还要具有底部/右侧的属性.如果省略任何属性,则可绘制对象将被缩放以触摸相应的边缘,因此圆形可以呈现为椭圆形或偏心圆形.

Note that it's important to have attributes not only for top/ left but also for bottom/ right. If you leave out any attributes then the drawable will be scaled to touch the respective edge, so the circle shape could be rendered as an oval or as an off-center circle.

这篇关于我的图层列表可绘制怎么了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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