我正在创建一个主屏幕小部件。按下并聚焦时,小部件应获得“正常”背景颜色。在其他情况下是透明的。
因此我定义了一个选择器
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true"
android:drawable="@drawable/widget_back_pressed" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="@drawable/widget_back_pressed" />
<item android:state_focused="true" android:drawable="@drawable/widget_back_selected" />
<item android:state_focused="false" android:state_pressed="false"
android:drawable="@drawable/transparent" />
除了聚焦状态外,一切都很好。如果小部件位于主屏幕上并且使用d-pad或键盘(在模拟器中)聚焦,则它将无法获得我选择的颜色。压制颜色很好。
怎么了?
谢谢和最诚挚的问候,
直到
答案 0 :(得分:0)
我遇到了同样的问题。我通过使我的ImageView可聚焦并确保其父布局不是来修复它。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/transparent" >
<RelativeLayout
android:id="@+id/relativeLayoutWidget1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/logo"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxHeight="72dp"
android:maxWidth="72dp"
android:scaleType="fitXY"
android:background="@drawable/btn_widget_background"
android:src="@drawable/widget_icon" />
</RelativeLayout>
</LinearLayout>
还要确保将onClickPendingIntent正确设置为可聚焦项目,否则您的窗口小部件将无法使用硬件按钮进行单击。所以对于这种情况,在AppWidgetProvider中:
views.setOnClickPendingIntent(R.id.logo, pendingIntent);