我正在尝试创建一个AppWidget,其中TextView的背景颜色以指定的周期间隔随机变化.
TextView在布局xml文件中定义为
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/message"
android:background="#ff99ff"
android:text="Hello Widget" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
在更新方法中,我已将布局加载为
RemoteViews remoteView=new RemoteViews(context.getPackageName(),R.layout.widget_message);
Run Code Online (Sandbox Code Playgroud)
要更改TextView的背景,我使用以下语句
remoteView.setInt(R.id.message, "setBackgroundResource", R.color.col_1);
Run Code Online (Sandbox Code Playgroud)
但我得到一个小部件说问题加载小部件.如果我删除上面的行一切正常.
LogCat说:
updateAppWidget使用错误视图找不到任何视图
android.widget.RemoteViews $ ActionException:view:android.widget.TextView无法使用RemoteViews方法:setBackgroundResource(int)
小智 62
试试这个它会工作正常.
remoteView.setInt(R.id.message, "setBackgroundColor",
android.graphics.Color.BLACK);
Run Code Online (Sandbox Code Playgroud)
如果要设置文本本身的颜色,请使用
remoteViews.setInt(R.id.tvAmountThisYear, "setTextColor",
android.graphics.Color.RED);
Run Code Online (Sandbox Code Playgroud)
小智 7
如果你有一些形状作为textview的背景,在某些可绘制资源中定义背景,那么你可以使用
remoteViews.setInt(R.id.change,"setBackgroundResource", R.drawable.my_drawable_new);
Run Code Online (Sandbox Code Playgroud)
在上面的代码语句中,R.id.change是带有一些后台资源的TextView,你在drawable文件夹中定义了一些资源(my_drawable和我的drawable_new).
<TextView
android:id="@+id/change"
android:background="@drawable/my_drawable">
</TextView
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30351 次 |
| 最近记录: |