使用 9 个补丁可绘制动态更改 AppWidget 的背景
RemoteViews
类中没有 setBackground()
方法,因此我使用了以下解决方法:
- 为我的应用程序小部件创建了一个
FrameLayout
ImageView
作为背景视图。 - 使用
setImageViewResource()
方法更改了ImageView
图像。
不幸的是,当涉及 9-patch 可绘制对象时,此方法不起作用。此外,当 ImageView
的 android:src
属性指向 9-patch 时 - 它也不起作用。有没有办法使用 9 补丁可绘制对象以编程方式更改 AppWidget 的背景图像?提前致谢。
编辑 将 9-patch 设置为 XML 中的初始背景:
<ImageView
android:id="@+id/small_widget_layout_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_lime" />
当我使用 android:src="@drawable/background_lime"
时,它无法正确拉伸图像,此代码工作正常。以及从 AppWidgetProvider
onUpdate
方法更改背景的代码:
views.setImageViewResource(R.id.small_widget_layout_bg,
R.drawable.backgroung_lime);
这不会将图像拉伸为 9 块。
There is no setBackground()
method in the RemoteViews
class, so I've used the following workaround:
- Created a
FrameLayout
for my app widget with anImageView
as the background view. - Changed the
ImageView
image usingsetImageViewResource()
method.
Unfortunately, when it comes to 9-patch drawables this method does not work. Also when an ImageView
's android:src
attribute points to 9-patch - it doesn't work too. Is there any way to change the AppWidget
's background image programatically using a 9-patch drawable? Thanks in advance.
EDIT
Settings the 9-patch as initial background in the XML:
<ImageView
android:id="@+id/small_widget_layout_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_lime" />
When I use android:src="@drawable/background_lime"
it doesn't stretch the image properly, this code works fine. And the code to change the background from the AppWidgetProvider
onUpdate
method:
views.setImageViewResource(R.id.small_widget_layout_bg,
R.drawable.backgroung_lime);
This does not stretch the image as a 9-patch.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个答案是在上面的评论中诊断出来的...
RemoteView
不允许访问View.setBackground()
,所以你使用android:src 的解决方法
属性很好,只要将ImageView
的android:scaleType
属性设置为 fitXY。ImageView
不会拉伸它的前景图像,除非你告诉它。This answer was diagnosed in the above comments...
RemoteView
doesn't allow access toView.setBackground()
, so your workaround of using theandroid:src
property of anImageView
is good, providing thatthe android:scaleType
property is set to fitXY.ImageView
won't stretch it's foreground image unless you tell it to.如果您发现此内容微不足道或无关紧要,请忽略,但您不能尝试(假设您正在处理小部件):
小部件<声明不同的布局(
xml
) /代码>。remoteView's 源(
layout.id
),而不是尝试更改所选布局。据我所知,这是解决此类问题的最常见方法。这对于我自己注意到的两个简单的事情来说并不完美:
但是只要你的 9 补丁文件是同样是静态资源,n 很痛苦,但理论上仍然可以管理。
我也很想为此找到一种简单的方法...
这种方法可能也不适合您,因为它是基本上是艰难的方式。但这仍然是一个选择。
建议#2
您尝试过使用该方法吗?
public void setInt (int viewId, String methodName, int value)
来自另一个问题:更改remoteView ImageView背景
Please ignore if u find this trivial or irrelevant, but canT you try (assuming you are dealing with widgets):
xml
)for yourwidget
.remoteView'
s source (layout.id
) instead of trying to make alterations to the selected layout.AFAIK, this is the most common approach to solving such problems. This is not perfect for two simple things I could note myself:
But as long as your 9-patch files are also static resources, n is painful but still theoretically manageable.
I'd also love to find an easy way for this one...
This approach may not be an option for you also because it is basically the hard way. But it s an option nonetheless.
Suggestion #2
Have you tried using the method?
public void setInt (int viewId, String methodName, int value)
From another question: Change remoteView ImageView background