Creating an empty Drawable in Android

前端 未结 8 896
陌清茗
陌清茗 2021-02-05 00:15

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

相关标签:
8条回答
  • 2021-02-05 00:37

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

    0 讨论(0)
  • 2021-02-05 00:41

    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.

    0 讨论(0)
  • 2021-02-05 00:43
    <shape />
    

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

    <?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:

    <?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> 
    
    0 讨论(0)
  • 2021-02-05 00:47

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

    0 讨论(0)
  • 2021-02-05 00:53

    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.

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

    0 讨论(0)
  • 2021-02-05 00:57

    @null in XML has the same effect as using a null as a Drawable in Java.

    0 讨论(0)
提交回复
热议问题