简体   繁体   English

Android主屏幕小部件中的ImageButton

[英]ImageButton in Android homescreen widget

I have a homescreen widget with an imagebutton. 我有一个带有图像按钮的主屏幕小部件。 I have the button working with a pending intent, but I can't seem to figure out how to change the button image when it is pressed. 我有一个处理未决意图的按钮,但我似乎无法弄清楚如何在按下时更改按钮图像。

I tried using a selector and it works in my widget test activity, but not in the remoteview. 我尝试使用选择器,它适用于我的小部件测试活动,但不适用于remoteview。

How could I implement this functionality in the home screen widget? 我怎样才能在主屏幕小部件中实现此功能?

您可以在远程视图中设置不同的图像。

remoteView.setInt(R.id.buttonimg, "setImageResource", R.drawable.button_on);

I was able to get it working by moving the selector inside it's own XML file and putting in res/drawable and then referencing that xml file as my resource for my imagebutton in the app-widget's layout xml. 我能够通过将选择器移动到其自己的XML文件中并放入res / drawable然后在app-widget的布局xml中引用该xml文件作为我的imagebutton的资源来实现它。

buttonimg.xml: buttonimg.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
 android:state_pressed="false"
 android:drawable="@drawable/button_off" />
<item
 android:state_pressed="true"
 android:drawable="@drawable/button_on" />
<item  
 android:drawable="@drawable/button_off" />
</selector>

widget_layout.xml widget_layout.xml

<ImageButton
android:id="@+id/buttonimg"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_x="0px"
android:layout_y="2px"
android:clickable="true"
android:src="@drawable/button"
>

I now have a different problem, however. 然而,我现在有一个不同的问题。 The selector only allows me to change the image onPress. 选择器只允许我更改onPress上的图像。 On release, the image changes back >:-( 在发布时,图像变回> :-(

How do I get the image state change on press and stay in that state until next press (like the ToggleButton)? 如何在按下时保持图像状态更改并保持该状态直到下一次按下(如ToggleButton)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM