Ste*_*anM 3 android android-layout
我一直在寻找如何使小部件的背景更改颜色的方法,我试图在活动中放置一个适合该图标的图标并对其进行更改,但这种方法不起作用。我尝试创建一个可以更改它的服务,但是那似乎太过残酷了,对我不起作用。有人说将其更改为可绘制对象。
这是我的代码,主要活动
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
Android清单
<?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>
Run Code Online (Sandbox Code Playgroud)
颜色资源
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="white">#FFFFFF</color>
</resources>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
我实际上需要它来更改随机颜色,但它甚至不会更改一种颜色。我如何改变它?
在定义主窗口小部件布局的布局中,我必须添加以下内容: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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3462 次 |
| 最近记录: |