在 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
Creating an empty Drawable in 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/empty
but 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/empty
when making an empty drawable. For me, @android:color/transparent
was 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
@null
in XML has the same effect as using a null
as a Drawable
in Java.
@null
在 XML 中与在 Java 中使用 anull
作为 a具有相同的效果Drawable
。
回答by Andrey Timofeev
Use @android:color/transparent
and 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 Drawable
image, you may use ShapeDrawable
with 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