Android 设置小部件布局的Alpha/不透明度

Android 设置小部件布局的Alpha/不透明度,android,android-widget,xml-layout,Android,Android Widget,Xml Layout,我想设置LinearLayout背景的不透明度,我发现如果我使用普通布局,这很好,但是在android小部件的情况下,我们必须设置不同的布局,我编写了以下代码 public class AlphaLayout extends LinearLayout { public AlphaLayout (Context context) { super(context); // TODO Auto-generated constructor stub } public AlphaLay

我想设置
LinearLayout
背景的不透明度,我发现如果我使用普通布局,这很好,但是在
android小部件的情况下,我们必须设置不同的布局,我编写了以下代码

public class AlphaLayout extends LinearLayout {

public AlphaLayout (Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

public AlphaLayout (Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

@Override
protected void onAnimationStart() {
    AlphaAnimation alphax = new AlphaAnimation(0.5F, 0.5F);
    alphax.setDuration(0);
    alphax.setFillAfter(true);
    this.startAnimation(alphax);
    super.onAnimationStart();
}
}

我对应的小部件xml布局是

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/widget_margin" >

<com.exercise.HelloWidget.AlphaLayout 
    android:id="@+id/myWidgetLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="@drawable/appwidget_dark_bg"
    android:orientation="horizontal" >
    <TextView
        android:id="@+id/song_info"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:text="Hello"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/white" />

    <TextView
        android:id="@+id/timeWidget"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:text="03:30"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/white" />

    
   
</com.exercise.HelloWidget.AlphaLayout>

运行项目后,当我将小部件添加到主屏幕时,它会显示一个错误
加载小部件时出现问题


你能告诉我我做错了什么吗?非常感谢您的帮助。

您不能在主屏幕上使用自定义视图/小部件


与典型视图相比,主屏幕小部件中使用的RemoveView的功能数量有限

主屏幕上不能有自定义视图/小部件

与典型视图相比,主屏幕小部件中使用的RemoveView的功能数量有限

如中所述:
RemoteView
支持的类不能扩展。因此,您不能使用
AlphaLayout

对于任何布局背景,都可以使用。您希望您的图像是透明的。这(据我所知)是无法做到的

然而,这是一个良好的开端:

  • 使用线性布局而不是AlphaLayout
  • 添加一个作为其背景:
    android:background=“@drawable/widget\u background”
  • 然后,根据需要设置背景。这里有一个建议:

    res/drawable/widget_background.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/Corners">
        <gradient android:startColor="#CC111111" android:endColor="#CC7f7f7f" android:angle="45"/>
        <padding android:left="4dp" android:top="1dp" android:right="4dp" android:bottom="1dp" />
        <corners android:radius="4dp" />
        <stroke android:width="2dp" android:color="#FFAfAfAf"/>
    </shape>
    
    
    
我相信这是制作小部件布局的标准方法。祝你好运

如中所述:
RemoteView
支持的类不能扩展。因此,您不能使用
AlphaLayout

对于任何布局背景,都可以使用。您希望您的图像是透明的。这(据我所知)是无法做到的

然而,这是一个良好的开端:

  • 使用线性布局而不是AlphaLayout
  • 添加一个作为其背景:
    android:background=“@drawable/widget\u background”
  • 然后,根据需要设置背景。这里有一个建议:

    res/drawable/widget_background.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/Corners">
        <gradient android:startColor="#CC111111" android:endColor="#CC7f7f7f" android:angle="45"/>
        <padding android:left="4dp" android:top="1dp" android:right="4dp" android:bottom="1dp" />
        <corners android:radius="4dp" />
        <stroke android:width="2dp" android:color="#FFAfAfAf"/>
    </shape>
    
    
    

我相信这是制作小部件布局的标准方法。祝你好运

那么,如果我想自定义它,我应该怎么做?你应该使用现有Android小部件集的图像和颜色,如ImageView等,但是如果你不能编写自己的自定义视图并将其放入主屏幕小部件中。那么如果我想自定义它,我应该怎么做?你应该使用现有Android小部件集的图像和颜色,如ImageView等,但是,如果您无法编写自己的自定义视图并将其放入主屏幕小部件中,是否需要半透明的
线性布局
?如60%透明?是的,要为已在
LinearLayout
背景上设置的图像设置不透明度。要半透明
LinearLayout
?如60%透明?是的,要将不透明度设置为已在
LinearLayout
背景上设置的图像。