我想设置LinearLayout
的背景不透明度,我发现如果我使用普通布局,这个解决方案可以很好地工作,但在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();
}
}
我对应的小部件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文档中所述:您不能扩展RemoteView
支持的类。因此,您不能使用AlphaLayout
类。
对于任何布局背景,都可以使用十种不同的绘图。您希望您的图像是透明的。这(据我所知)是不可能做到的。
然而,这是一个良好的开端:
- 使用LinearLayout而不是AlphaLayout
- 添加可绘制形状作为背景:
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>
我相信这是制作小部件布局的标准方法。祝你好运
主屏幕上不能有自定义视图/小部件。
与典型视图相比,主屏幕窗口小部件中使用的RemoveViews的功能数量有限。