【问题标题】:Android Change Widget Background ImageAndroid 更改小部件背景图片
【发布时间】:2011-10-30 08:03:06
【问题描述】:

过去两天一直在努力更改我的小部件的背景,基于一些 if 语句(现在删除只是想从类中更改小部件背景)这里是我的源代码如下。不过怎么回事,我之前已经更改了图像,例如背景,但无法让它为我的小部件工作,谢谢。顺便说一下,这是我最近的尝试

//Widget Provider Class
public class WidgetProvider extends AppWidgetProvider {

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;

        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];

            Intent intent = new Intent(context, com.widget.WidgetDialog.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
            views.setOnClickPendingIntent(R.id.widget, pendingIntent);

            appWidgetManager.updateAppWidget(appWidgetId, views);
            views.setImageViewBitmap(R.id.widget, ((BitmapDrawable)context.getResources().getDrawable(R.drawable.widget_background)).getBitmap());

        }
    }
}



//Widget Layout XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button android:id="@+id/widget"
    android:background="#00000000"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    </Button>
</LinearLayout>

【问题讨论】:

  • 我尝试了同样的方法,但从未找到解决方案,似乎在布局膨胀后您无法更改小部件的背景。这可能是由于 RemoteView-Limitations。我想要一个实心小部件(alpha=255)和一个半透明的(alpha=128)。作为一种解决方案,我创建了两个具有相应布局的布局,并在我想切换背景时切换了这些布局。
  • 不幸的是我最终做的事情意味着我必须添加 10 个不同的 xml,因为我只是想根据不同的 if 更改 5 个不同的图像,所以需要 10 个似乎效率很低,但你能做什么我听到不过冰淇淋三明治会给我们更好的小部件控制

标签: android background bitmap widget drawable


【解决方案1】:

您可以使用以下技巧:使用 ImageView 作为背景,而不是 View 的“背景”属性,并将 scaleType 设置为“fitXY”。像这样:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">    

<ImageView
    android:id="@+id/backgroundImage"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/some_background"
    android:scaleType="fitXY"/>

<RelativeLayout
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent">       

        <Button
            android:text="some button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <!-- all your views -->

</RelativeLayout>

</FrameLayout>

现在您可以在运行时切换 ImageView 源代码:

//updating current widget
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews views = new RemoteViews(getPackageName(), R.layout.widget_layout);

views.setImageViewResource(R.id.backgroundImage, R.drawable.some_other_background);

appWidgetManager.updateAppWidget(widgetId, views);

【讨论】:

  • 帮了我一个大忙,非常感谢!
  • @Vladimir Sizov 如何从 url 加载图片?
【解决方案2】:

尝试使用setImageViewResource()

remoteViews.setImageViewResource(R.id.widget, R.drawable.widget_background);

谢谢

【讨论】:

  • 我过去试过这个,因为它对我来说最有意义,但写你建议的错误应该是 views.setImageViewResource(R.id.widget, R.drawable.widget_background) ;然后它也不起作用,所以在 xml 中我将背景设置为 #00000000 但是我在课堂上设置的内容并没有显示我只有一个清晰的小部件还有其他建议吗?谢谢
【解决方案3】:

有人有其他想法吗?真的很想弄清楚这一点

编辑:最终设置了不同的布局,目前有 10 种不同的布局用于 2 个不同的小部件效率低下,但实际上工作更多的是 hack

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    • 2012-10-16
    • 2014-03-04
    • 2018-02-03
    相关资源
    最近更新 更多