我在android中研究并创建了一些应用程序部件。因此,我有一个小部件,有6个图像视图,当用户单击其中一个时,小部件将启动应用程序。
问题在于用户没有反馈他的触摸是成功的(我的意思是,视图没有着色,因为没有其他的onTouchListener )。
我看到了很多小部件,当用户触摸时,这些小部件可以着色,但我不能这样做(在远程视图中,我无法通过视图直接获得访问权限)!我怎么能做到呢?
发布于 2014-04-07 15:13:32
您可以通过如下所示创建一个可绘制的自定义绘图来实现这一点,并将其设置为ImageView。当用户触摸ImageView时,会显示不同的颜色。
widget_item_image.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/black_overlay"
android:state_pressed="true"/>
<item android:drawable="@color/maroon" android:state_enabled="true"/>
</selector>widget_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@color/white">
<ImageView
android:id="@+id/imageView1"
android:layout_width="200dp"
android:layout_height="60dp"
android:src="@drawable/widget_item_image"
android:scaleType="fitXY"
android:layout_margin="10dp"/>
</LinearLayout>现在,您的小部件提供程序onUpdate()应该将小部件布局设置为远程视图,并向ImageView添加单击意图以启动活动
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,0);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_layout);
remoteViews.setOnClickPendingIntent(R.id.imageView1,pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);希望这能有所帮助。
https://stackoverflow.com/questions/22913061
复制相似问题