android - 设置小部件布局的 Alpha/Opacity

标签 android android-widget xml-layout

我想设置 LinearLayout 背景的不透明度,我找到了这个 solution如果我使用普通布局,它工作正常,但在 android-widgets 的情况下,我们必须设置不同的布局我写了以下代码

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();
}

而我对应的widget 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>

运行项目后,当我将小部件添加到主屏幕时,它显示错误 Problem loading widget

你能告诉我我做错了什么吗?我们将不胜感激。

最佳答案

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

对于任何布局背景,都有ten different drawables你可以使用。您希望您的图像是透明的。这(据我所知)不能用我们现有的资源来完成。

但是,这是一个好的开始:

  • 使用 LinearLayout 而不是 AlphaLayout。
  • 添加 Shape Drawable作为背景:android:background="@drawable/widget_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 - 设置小部件布局的 Alpha/Opacity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11661813/

相关文章:

android - 我们如何将三星银河皮肤添加到安卓模拟器中?

java - Android MediaRecorder stop() 未被调用

java - 如何在 Android 的 MVVM 中处理回调和状态

android - 将我的小部件与我的应用程序集成

android - 在 Android 布局设计中使用特殊符号 (<, >)

android - 方向改变时布局发生巨大变化

android - 如何为长文本设计 Chips 布局

java - android中java.lang.OutOfMemoryError的主要原因是什么

android - 在 Android 中放置 facebook 墙

android - 将 ImageView 从 URL 加载到主屏幕小部件的 RemoteView