Android小部件更改背景颜色

5

我一直在寻找如何更改小部件的背景颜色,我尝试放置一个适合活动的图标并更改它,但它没有起作用。我尝试创建一个可以更改它的服务,但那似乎是过度的,并且对我没有起作用。有人说要将其更改为可绘制对象。

所以这是我的代码, 主要活动

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

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

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

颜色资源

<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="white">#FFFFFF</color>
</resources>

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>

我需要它改变随机颜色,但它甚至无法改变一个颜色。该怎么办才能使它改变?
1个回答

12

在定义主widget布局的布局中,我需要添加以下内容:android:id="@+id/LineaRLayout1"。

然后在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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接