【问题标题】:Android Widget Backgroundcolor when focused聚焦时的Android小部件背景颜色
【发布时间】:2011-04-12 21:48:54
【问题描述】:

我正在创建一个主屏幕小部件。按下并聚焦时,小部件应获得“正常”背景颜色。其他情况下透明。

因此我定义了一个选择器

<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 或键盘(在模拟器中)聚焦,它将无法获得我选择的颜色。压色效果很好。

怎么了?

谢谢和最好的问候,

直到

【问题讨论】:

  • 应用小部件没有获得焦点,AFAIK。
  • @CommonsWare GoogleReader 的小部件改变了它的背景颜色

标签: android android-widget widget selector


【解决方案1】:

我遇到了同样的问题。我通过使我的 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-22
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    相关资源
    最近更新 更多