简体   繁体   中英

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 the 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.

据我所知,执行此操作的唯一方法是使用半透明的可绘制对象(可能是9patch),因为似乎无法通过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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM