开发者

Android Widget Backgroundcolor when focused

I am creating a homescreen widget. The widget should get the "normal" background colors when pressed and focussed. Transparent in the other cases.

Therefore I have defined 开发者_如何学运维a selector

<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" />

Mostly everything works fine except the focused state. If the widget is on the homescreen and focused with the d-pad or the keyboard (in the emulator) it will not get my chosen color. Pressed color works fine.

Whats wrong?

Thanks and best regards,

Till


I had the same problem. I fixed it by making my ImageView focusable and making sure its parent layouts were not.

<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>

Also make sure you set your onClickPendingIntent correctly to the focusable item, otherwise your widget won't be clickable with the hardware button. So for this case, in your AppWidgetProvider:

views.setOnClickPendingIntent(R.id.logo, pendingIntent);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜