开发者

Semi-transparent AppWidget?

I have an AppWidget where the layout has a LinearLayout at the root. The widget uses a background 9-patch to provide a frame for the widget.

How do I set the alpha channel on the widget?

I can't set it directly as the background attribute is being used to specify t开发者_Go百科he 9-patch. It's a LinearLayout not an ImageView, so I can't use setAlpha(), so how do I make it semi-transparent?

The transparency level will change dynamically, so I am unable to use a semi-transparent bitmap.


See the answers to How can I set an entire view's alpha value in api level 7 (Android 2.1)


As far as I can see the only way to do this is to use a semi-transparent drawable (perhaps a 9patch) as there appears to be no way to do it via RemoteViews.


You can set the transparency dynamically in a RemoteViews on Android 2.2 and above by using an imageview to display the background and taking advantage of the setAlpha method of imageview.

Use a relativelayout to position an image view behind the content and within the imageview set the src to your background image. Note that I'm setting the image to src and not background so I can use the setAlpha method.

The layout:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/MainLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/BackgroundImageView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_margin="6dip"
            android:cropToPadding="true"
            android:scaleType="fitXY"
            android:src="@drawable/widget_background" >
        </ImageView>

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/ContentLinearLayout"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="fill"
            android:layout_marginBottom="26dip"
            android:layout_marginLeft="14dip"
            android:layout_marginRight="14dip"
            android:layout_marginTop="14dip"
            android:clickable="true"
            android:clipChildren="false"
            android:clipToPadding="false"
            android:gravity="top|fill_horizontal"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/TextView01"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:gravity="center"
                android:text="loading..."
                android:textSize="18dip"
                android:textStyle="bold" >
            </TextView>
        </LinearLayout>

    </RelativeLayout>

When you're creating the RemoteViews for display in the widget you can use the generic setInt method to access the setAlpha method on the ImageView like so:

setInt(R.id.BackgroundImageView, "setAlpha", alpha);

I also use setImageViewResource to swap out the background image from preferences.

setImageViewResource(R.id.backgroundImageView, bgDrawable);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜