在 Android 中创建一个空的 Drawable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2234292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:04:59  来源:igfitidea点击:

Creating an empty Drawable in Android

android

提问by Kai

Creating a Drawable that is completely empty seems like a common need, as a place holder, initial state, etc., but there doesn't seem to be a good way to do this... at least in XML. Several places refer to the system resource @android:drawable/emptybut as far as I can tell (i.e., it's not in the reference docs, and aapt chokes saying that it can't find the resource) this doesn't exist.

创建一个完全空的 Drawable 似乎是一个常见的需求,作为占位符、初始状态等,但似乎没有一个好的方法来做到这一点......至少在 XML 中。有几个地方引用了系统资源,@android:drawable/empty但据我所知(即它不在参考文档中,并且 aapt chokes 说它找不到资源)这不存在。

Is there a general way of referencing an empty Drawable, or do you end up creating a fake empty PNG for each project?

是否有引用空 Drawable 的通用方法,或者您最终是否为每个项目创建了一个假的空 PNG?

回答by Emil

For me it didn't work when I tried to use @android:id/emptywhen making an empty drawable. For me, @android:color/transparentwas the one.

对我来说,当我尝试@android:id/empty在制作空的可绘制对象时使用它时它不起作用。对我来说,@android:color/transparent是一个。

回答by JulianSymes

I use an empty ShapeDrawable, i.e. create my own drawable/empty.xml containing just <shape/>

我使用一个空的ShapeDrawable,即创建我自己的 drawable/empty.xml 只包含<shape/>

回答by Hans

<shape />

creates an empty shape and does the trick for me. Or create a file empty_drawable.xml containing:

创建一个空的形状,并为我做的伎俩。或者创建一个文件 empty_drawable.xml 包含:

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

so you can use it elsewhere.

所以你可以在其他地方使用它。

You can also make a shape disappear when the view it is used in is not enabled by creating my_disablable_view.xml:

您还可以通过创建 my_disabable_view.xml 使形状在使用它的视图未启用时消失:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true">
        <shape android:shape="oval">
            <size android:height="10dp" android:width="10dp" />
            <solid android:color="@android:color/white" />
        </shape>
    </item>
    <item android:state_enabled="false">
        <shape />
    </item>
</selector> 

回答by laalto

@nullin XML has the same effect as using a nullas a Drawablein Java.

@null在 XML 中与在 Java 中使用 anull作为 a具有相同的效果Drawable

回答by Andrey Timofeev

Use @android:color/transparentand don't forgot add android:constantSize="true"on <selector>

使用@android:color/transparent不忘记加上android:constantSize="true"<selector>

回答by wirbly

For me, using @android:drawable/empty would show an error and prevent compiling. Using @android:id/empty fixed the error and let me compile, but showed "NullPointerException: null" in the Eclipse Layout editor. I changed it to @android:color/transparent and now everything is fine.

对我来说,使用 @android:drawable/empty 会显示错误并阻止编译。使用@android:id/empty 修复了错误并让我编译,但在 Eclipse 布局编辑器中显示“NullPointerException: null”。我将其更改为@android:color/transparent,现在一切正常。

I can't vote up yet or I would have up'ed Emil's answer.

我还不能投票,否则我会支持 Emil 的回答。

回答by John LeBoeuf-Little

Kai, I don't know why this is the case, but I've gotten the same issue. I think it might be related to the version of Android you're compiling for? In any case, I found that simply using @android:id/empty where you would use @android:drawable/empty does the trick.

凯,我不知道为什么会这样,但我遇到了同样的问题。我认为这可能与您正在编译的 Android 版本有关?无论如何,我发现只使用 @android:id/empty 就可以了。

回答by Benny

To create an empty Drawableimage, you may use ShapeDrawablewith transparent color:

要创建一个空Drawable图像,您可以使用ShapeDrawable透明颜色:

val shapeDrawable = ShapeDrawable(OvalShape())
shapeDrawable.paint.color = context.getColor(android.R.color.transparent)

If the image size is important, use:

如果图像大小很重要,请使用:

shapeDrawable.intrinsicWidth = 100
shapeDrawable.intrinsicHeight = 100