![](/img/trans.png)
[英]how to set rounded corners with background tansparency object in android shape xml
[英]Android How to set an xml shape as widget background?
如何在Widget中将xml中定义的形状设置为图像的背景? 这是我的尝试:
RemoteViews views = new RemoteViews(c.getPackageName(), R.layout.widget_layout);
Drawable dr = getResources().getDrawable(R.drawable.widgetshape); //class cast exception
Bitmap bp = ((BitmapDrawable)dr).getBitmap();
views.setImageViewBitmap(R.id.ImageView01, bp);
这会导致android.graphics.drawable.GradientDrawable类强制转换异常。
Bitmap icon = BitmapFactory.decodeResource(WidgetConfig.this.getResources(), R.drawable.widgetshape);
views.setImageViewBitmap(R.id.ImageView01, icon);
这会在awm.updateAppWidget(awID, views);
上引发NullPointerException错误awm.updateAppWidget(awID, views);
线。
Bitmap bp = ((BitmapDrawable)this.getResources().getDrawable(R.drawable.widgetshape)).getBitmap(); //Class Cast Exception
views.setImageViewBitmap(R.id.ImageView01, bp);
这会导致第一行出现android.graphics.drawable.GradientDrawable类强制转换异常。
您可以通过调用RemoteViews
实例的setInt
方法来实现:
views.setInt(R.id.layout, "setBackgroundResource", R.drawable.widgetshape);
为此,必须在小部件布局的View
(可能是根视图)(在xml中)中将@id
属性设置为"layout"
。
您还可以通过xml定义该视图的背景,例如:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/widgetshape">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.