[英]Android Widget Change Background Color
I have been searching for how to make the Background of a widget change color I tried putting an Icon that fit over the activity and change that, but it didn't work. 我一直在寻找如何使小部件的背景改变颜色我尝试放置一个适合活动的图标并改变它,但它没有用。 I tried Creating a service that would change it, but that seemed like over kill and didn't work for me. 我尝试创建一个可以改变它的服务,但这似乎过度杀戮而且对我不起作用。 Someone said to change it as a drawable. 有人说要改变它作为一个可绘制的。
So here is my code, Main Activity 所以这是我的代码,主要活动
public class MainActivity extends AppWidgetProvider {
private static int layoutId;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
RemoteViews updateViews;
for (int i = 0; i < appWidgetIds.length; i++) {
int appWidgetId = appWidgetIds[i];
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.activity_main);
layoutId = R.layout.activity_main;
Random rnd = new Random();
int bgcolor = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
//updating current widget
views.setInt(layoutId, "setBackgroundResource", R.color.colorPrimaryDark);
updateViews = new RemoteViews(context.getPackageName(),layoutId);
AppWidgetManager.getInstance(context).updateAppWidget(
new ComponentName(context, MainActivity.class), updateViews);
}
}
}
activity_main.xml activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@color/white"
tools:context="com.example.stephan.mywidget.MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/bgcolor"
android:scaleType="fitXY"/>
</RelativeLayout>
AndroidManifest AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.stephan.mywidget">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver android:name="MainActivity">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widget"/>
</receiver>
</application>
</manifest>
Color resources 色彩资源
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="white">#FFFFFF</color>
</resources>
Widget.xml Widget.xml
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dp"
android:minHeight="72dp"
android:updatePeriodMillis="28800000"
android:initialLayout="@layout/activity_main">
</appwidget-provider>
I actually need it to change random colors but it wont even change one color. 我实际上需要它来改变随机颜色,但它甚至不会改变一种颜色。 How do I get it to change? 我如何让它改变?
In the Layout that defined the main widget layout I had to add the following: android:id="@+id/LineaRLayout1" 在定义主要小部件布局的布局中,我必须添加以下内容:android:id =“@ + id / LineaRLayout1”
then in in the AppWidgetProvider the onUpdate ended up looking like this, all I am really showing here is how I changed the color 然后在AppWidgetProvider中,onUpdate最终看起来像这样,我真正在这里展示的是我如何改变颜色
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
Log.d(LOG_TAG, "Updating Example Widgets.");
// Perform this loop procedure for each App Widget that belongs to this
// provider
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
// Create an Intent to launch ExampleActivity
Intent intent = new Intent(context, WidgetActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.activity_main);
//try change color here with no luck, I tried activity_main,background, widget1
views.setInt(R.id.LineaRLayout1, "setBackgroundColor", Color.GREEN);
views.setOnClickPendingIntent(R.id.button, pendingIntent);
// Tell the AppWidgetManager to perform an update on the current app
// widget
appWidgetManager.updateAppWidget(appWidgetId, views);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.