gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:06:01 25 4
gpt4 key购买 nike

我想设置 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com