Android AppWidget TextView:如何在运行时设置背景色 [英] Android AppWidget TextView: How to set background color at run time

查看:125
本文介绍了Android AppWidget TextView:如何在运行时设置背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个AppWidget,其中TextView的背景颜色以指定的周期间隔随机变化.

I am trying to create an AppWidget, in which the background color of a TextView changes at random at specified periodic interval.

TextView在布局xml文件中定义为

The TextView is defined in layout xml file as

<?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>

在更新方法中,我已将布局加载为

In update method, i have loaded the layout as

RemoteViews remoteView=new RemoteViews(context.getPackageName(),R.layout.widget_message);

要更改TextView的背景,我使用了以下语句

To change the background of TextView i used the following statement

remoteView.setInt(R.id.message, "setBackgroundResource", R.color.col_1);

但是我得到一个小部件,提示加载小部件时出现问题.如果我删除上面的行,一切正常.

But i am getting a widget saying problem loading widget. If i remove the above line everything works fine.

LogCat说:

updateAppWidget找不到任何视图,使用错误视图

updateAppWidget couldn't find any view, using error view

android.widget.RemoteViews $ ActionException:视图:android.widget.TextView无法在RemoteViews中使用方法:setBackgroundResource(int)

android.widget.RemoteViews$ActionException: view: android.widget.TextView can't use method with RemoteViews: setBackgroundResource(int)

推荐答案

尝试一下可以正常工作.

Try this it will work fine.

remoteView.setInt(R.id.message, "setBackgroundColor", 
        android.graphics.Color.BLACK);

这篇关于Android AppWidget TextView:如何在运行时设置背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆