Android AppWidget TextView:如何在运行时设置背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2453543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:54:31  来源:igfitidea点击:

Android AppWidget TextView: How to set background color at run time

android

提问by Amit

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

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

The TextView is defined in layout xml file as

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>

In update method, i have loaded the layout as

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

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

To change the background of TextView i used the following statement

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

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 says:

LogCat 说:

updateAppWidget couldn't find any view, using error view

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

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

android.widget.RemoteViews$ActionException:视图:android.widget.TextView 不能使用 RemoteViews 的方法:setBackgroundResource(int)

回答by Abhishek Jain

Try this it will work fine.

试试这个它会工作得很好。

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

回答by AntonSack

If you want to set the color of the text itself, use

如果要设置文本本身的颜色,请使用

remoteViews.setInt(R.id.tvAmountThisYear, "setTextColor",
                android.graphics.Color.RED);

回答by T.M

If you have some shape as the background of the textview, where the background is defined in some drawable resource, then you can use

如果你有一些形状作为 textview 的背景,其中背景是在一些可绘制资源中定义的,那么你可以使用

remoteViews.setInt(R.id.change,"setBackgroundResource", R.drawable.my_drawable_new);

In above code statement, R.id.change is the TextView with some background resource and you have defined some resources (my_drawable and my drawable_new) in your drawable folder.

在上面的代码语句中,R.id.change 是带有一些背景资源的 TextView,并且您在 drawable 文件夹中定义了一些资源(my_drawable 和 my drawable_new)。

<TextView
    android:id="@+id/change"
    android:background="@drawable/my_drawable">
</TextView

回答by peter

contentView.setInt(R.id.tv_contactText, "setBackgroundColor", Color.parseColor(hexColor));

contentView.setInt(R.id.tv_contactText, "setBackgroundColor", Color.parseColor(hexColor));

回答by thomasd

As of android 2.2 this method can be called, not before.

从 android 2.2 开始,可以调用此方法,而不是之前。

回答by mobibob

Tomas is correct. My solution is to make two views with the respective backgrounds and make one INVISIBLE and the other one VISIBLE. Of course this only works with a small number of backgrounds, e.g., "green" and "red" that might indicate some state.

托马斯是对的。我的解决方案是用各自的背景制作两个视图,并使一个不可见,另一个可见。当然,这只适用于少数背景,例如,可能表示某种状态的“绿色”和“红色”。

回答by DavidG

What i find weird about this is that it works great on my nexus one ( 2.2 ), but not at all on an HTC Tattoo ( 1.6 ). I'm going to try and run some emulator tests and see if it's not just another case of HTC lazily implementing some underlying UI rendering code, which i already found with the Tattoo ( Layouts render differently than on stock Android 1.6 ).

我对此感到奇怪的是,它在我的 nexus one (2.2) 上效果很好,但在 HTC 纹身 (1.6) 上根本不起作用。我将尝试运行一些模拟器测试,看看这是否不仅仅是 HTC 懒惰地实现一些底层 UI 渲染代码的另一种情况,我已经在纹身中发现了这些代码(布局渲染与库存 Android 1.6 不同)。

What device were you testing this on?

你在什么设备上测试这个?

回答by Zulaxia

The emulator will let this go through when you run a 2.2 emulator, so I guess the restriction is lifted as of 2.2.

当您运行 2.2 仿真器时,仿真器将允许它通过,所以我想从 2.2 开始取消限制。

回答by Tomá? Hubálek

The reason is that via RemoteViews you can call just limited amount of methods. In case that it is prohibited you get message like this.

原因是通过 RemoteViews 您只能调用有限数量的方法。如果被禁止,您会收到这样的消息。

Tom

汤姆