2011-04-12 21 views
0

我正在創建一個主屏幕小工具。當按下並聚焦時,小部件應該獲得「正常」的背景顏色。在其他情況下透明。安卓小工具集中時的背景色

所以我定義了一個選擇

<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

應用程序小部件不會獲得焦點AFAIK。 – CommonsWare 2011-04-13 02:23:35

+0

@CommonsWare GoogleReader的小部件會改變它的背景顏色 – Till 2011-04-13 08:11:50

回答

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