开发者

Transparent widget background?

On Android widgets, how can I give the background a color, yet make is transparent at the same time? Please see the background on the clock at the screenshot. It's a black, yet transparent background. How can I do that o开发者_JS百科n my own widget? Can I maybe also reference system colors, like the one on the screenshot from HTC Sense? Thanks for any hint!

Transparent widget background?


You have to specify an alpha-channel (=transparency channel) for your ressources.

If you are using a standard color in your XML res (e.g. from your colors.xml) to fill the background, you may specify alpha by changing the first two numbers. Example format:

<color name="transparent_red">#99FF0000</color>

The 99 here indicates the alpha value, change it to FF will produce solid red, changing it to 00 will make the entire color invisible. Just test around with this by changing the value inbetween.

For the details about this, please look here.

If you want to use an actual picture/bitmap, like the sense one does, you have to create PNG-images with an alpha channel specified. That depends on which software you are using to create your bitmaps. I recommed vector graphic programs like Illustrator or Inkscape for doing this, they have an alpha value when you specify a color. After you created it there, export the image as a PNG and add it to your apps ressources.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@android:color/transparent">

    <ImageView
        android:id="@+id/widget_image"
        android:layout_width="110sp"
        android:layout_height="110sp"
        android:layout_gravity="center"
        android:src="@drawable/appicon" />
</LinearLayout>

Just set background of layout to transparent. Add below code. This worked for me and Its a widget.

android:background="@android:color/transparent"


android:background="#TTRRGGBB -- RR, GG, BB are the (hex) red, green, blue components, and TT is the transparency component. Use TT = 80 (hex) for 50% transparency, FF for no transparency, 00 for fully transparent.

Or use a png with transparency components, as your screenshot appears to use.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜