2011-05-13 212 views
1

我有一个AppWidget,其中的布局在根上有一个LinearLayout。小部件使用背景9修补程序为小部件提供框架。半透明AppWidget?

如何在窗口小部件上设置Alpha通道?

由于使用背景属性指定9修补程序,因此无法直接设置它。这是一个LinearLayout而不是一个ImageView,所以我不能使用setAlpha(),那么如何让它变成半透明?

透明度级别会动态变化,所以我无法使用半透明位图。

+0

set alpha可用于线性布局超类。你在使用这个功能时遇到什么问题 – 2011-05-13 18:30:51

+0

我应该提到两件事。一,我需要设置一个小部件的透明度,所以我使用RemoteViews。其次,我支持平台5及更高版本。我开始怀疑是否唯一的方法是使用静态,半透明的9补丁:( – 2011-05-14 09:44:03

回答

0

至于我可以看到这样做的唯一方法是使用一个半透明绘制(也许是9patch),因为似乎没有办法通过RemoteViews做。

0

通过使用imageview显示背景并利用imageview的setAlpha方法,您可以在Android 2.2及更高版本的RemoteViews中动态地设置透明度。

使用relativelayout将图像视图放置在内容后面,并在imageview中将src设置为背景图像。请注意,我将图像设置为src而不是背景,因此我可以使用setAlpha方法。

布局:

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

当你在widget创建RemoteViews显示您可以使用通用SETINT方法来访问对的ImageView的setAlpha方法,像这样:

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

我也使用setImageViewResource从首选项中取出背景图像。

setImageViewResource(R.id.backgroundImageView, bgDrawable);