0

我有一个带有按钮作为其子项的线性布局。当它们获得焦点时,我需要改变按钮背景。这些按钮在触摸模式下是可聚焦的。

我已将以下选择器 xml 设置为按钮。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true"  
           android:drawable="@drawable/tile_focused" />
    <item android:drawable="@drawable/tile" /> <!-- default -->
</selector>

更新

if (layout == null) {
        layout = new LinearLayout(this);
        RelativeLayout.LayoutParams rlp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        rlp.addRule(RelativeLayout.BELOW, R.id.toplogo);
        layout.setLayoutParams(rlp);
        layout.setId(wordLayoutID);
        layout.setBackgroundColor(Color.WHITE);
        mainLayout.addView(layout, rlp);
    } 

    Drawable tileBG = getResources().getDrawable(R.drawable.selector_tile);
    for (int i = 0; i < word.length(); i++) {
        Button btntile = new Button(this);
        LinearLayout.LayoutParams rlp =  new LinearLayout.LayoutParams(40, 40);
        btntile.setId(inputViewsIds+i);
        btntile.setBackgroundDrawable(tileBG);
        btntile.setFocusableInTouchMode(true);
        layout.addView(btntile, rlp);
    }

问题是当我将焦点设置到任何一个按钮时,所有按钮的背景都会变为“tile_focused”。我猜问题出在选择器xml中。这里有什么问题?

4

2 回答 2

2

简而言之,您android:state_pressed="true"可能还想要混合其他一些东西。我不是 100% 确定,但请继续阅读。

对于这样的问题,我总是查看源代码以了解样式和主题是如何工作的。ListView 样式的源代码如下。

查看源代码<item name="android:listSelector">@android:drawable/list_selector_background</item>似乎是我们想要深入研究的地方。

这是该可绘制对象的源代码。这应该提供指导来完成你想要的。

https://github.com/android/platform_frameworks_base/blob/master/core/res/res/drawable/list_selector_background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:state_window_focused="false" android:drawable="@color/transparent" />

  <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
  <item android:state_focused="true"  android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/list_selector_background_disabled" />
  <item android:state_focused="true"  android:state_enabled="false"                              android:drawable="@drawable/list_selector_background_disabled" />
  <item android:state_focused="true"                                android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition" />
  <item android:state_focused="false"                               android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition" />
  <item android:state_focused="true"                                                             android:drawable="@drawable/list_selector_background_focus" />

</selector>

https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/styles.xml#L614

<style name="Widget.ListView" parent="Widget.AbsListView">
    <item name="android:listSelector">@android:drawable/list_selector_background</item>
    <item name="android:cacheColorHint">?android:attr/colorBackgroundCacheHint</item>
    <item name="android:divider">@android:drawable/divider_horizontal_dark_opaque</item>
</style>

<!-- Abstract ListView, nothing of importance here. -->
<style name="Widget.AbsListView">
    <item name="android:scrollbars">vertical</item>
    <item name="android:fadingEdge">vertical</item>
</style>
于 2012-04-17T18:36:58.910 回答
1

这种行为的根本原因是我用于所有按钮的通用 tile 可绘制参考。我改变了它并为每个按钮加载了单独的drawable。那解决了它。

谢谢大家。

于 2012-04-18T06:02:22.027 回答