视图背景的选择器在聚焦时应用于所有兄弟姐妹

时间:2012-04-17 18:03:36

标签: android focus android-selector

我有一个linearLayout,按钮作为其子项。我需要按钮背景,以便在获得焦点时进行更改。按钮可在触摸模式下聚焦。

我已将以下选择器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中。这有什么不对?

2 个答案:

答案 0 :(得分:2)

简而言之,您似乎希望android:state_pressed="true"也可能混合其他一些东西。我不是百分百肯定,而是继续阅读。

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

查看来源<item name="android:listSelector">@android:drawable/list_selector_background</item>看起来就像我们想深入了解的那样。

这是drawable的源代码。这应该为完成你想要的东西提供指导。

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>

答案 1 :(得分:1)

此行为的根本原因是我用于所有按钮的常见tile可绘制引用。我更改了它并为每个按钮加载了单独的drawable。修好了。

感谢大家。