set shape background to transparent in android

2020-06-12 02:48发布

问题:

I have a shape drawable that I want to use as a background. I want the shape to be transparent. But so far it's not. How do I do that? Here is what I have:

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

    <solid android:color="#80000000" />

    <stroke
        android:width="1dip"
        android:color="#000000" />

    <corners android:radius="10dp" />

</shape>

I expected the line <solid android:color="#80000000" /> to do the trick.

EDIT:

In manifest, I set android:theme="@android:style/Theme.Dialog". Could that be the cause of the problem? I am basically trying to get this view on top of another. Changing to 00 or FF or whatever else does not work.

回答1:

if you want set shape background to transparent you need to set this color

<solid android:color="#00000000" />


回答2:

for creating a color including an opacity effect, you can create a color with taking the alpha channel into account.

<resources>
<color name="translucent_grey">#88cccccc</color>
</resources>

a quote from another User about the color formats:

When defining the color of a view in android, the format can be either #RRGGBB or #AARRGGBB, where AA is the hex alpha value. FF would be fully opaque and 00 would be full transparent.

more information about this technique can be found in this thread: How to Set Opacity (Alpha) for View in Android

Regards, Jim



回答3:

You can use this :

<solid
    android:color="@android:color/transparent"/>

This is working for me.



回答4:

I found the answer. Direct quote:

The easiest way that I have found is to set the activity's theme in the AndroidManifest to android:theme="@android:style/Theme.Holo.Dialog" then in the activity's onCreate method call getWindow().setBackgroundDrawable(new ColorDrawable(0));

from @DrewLeonce in Android: how to create a transparent dialog-themed activity