2

我希望我的 Button 默认使用透明背景绘制,按下时使用深蓝色背景绘制,选择时使用浅蓝色背景绘制。我将按钮的背景设置为下面的选择器,并在Button.setSelected(!Button.isSelected())单击按钮时调用。按下状态正常工作,但选择时 Button 以透明背景绘制。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/tranpsshape" /> 
  <item android:state_window_focused="false" android:state_enabled="false" android:drawable="@drawable/tranpsshape" /> 
  <item android:state_pressed="true" android:drawable="@drawable/pressedbuttonshape" /> 
  <item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/selectedshape" /> 
  <item android:state_enabled="true" android:drawable="@drawable/tranpsshape" /> 
  <item android:state_focused="true" android:drawable="@drawable/tranpsshape" /> 
  <item android:state_selected="true" android:drawable="@drawable/selectedshape" /> 
  <item android:drawable="@drawable/tranpsshape" /> 
</selector>

选定形状.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#A3DAF2"/>
</shape>

按下按钮形状.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#4ABDE8/>
</shape>

转换形状.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#00000000"/>

</shape>
4

2 回答 2

5

根据文档

在每次状态更改期间,从上到下遍历状态列表,并且将使用与当前状态匹配的第一个项目 - 选择不是基于“最佳匹配”,而只是第一个满足最低标准的项目状态。

所以你需要放在state_selected上面state_focused。所以你的文件看起来像这样

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/tranpsshape" /> 
    <item android:state_window_focused="false" android:state_enabled="false" android:drawable="@drawable/tranpsshape" /> 
    <item android:state_pressed="true" android:drawable="@drawable/pressedbuttonshape" /> 
    <item android:state_selected="true" android:drawable="@drawable/selectedshape" />         
    <item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/selectedshape" /> 
    <item android:state_enabled="true" android:drawable="@drawable/tranpsshape" /> 
    <item android:state_focused="true" android:drawable="@drawable/tranpsshape" /> 
    <item android:drawable="@drawable/tranpsshape" /> 
</selector>
于 2012-09-07T15:24:31.697 回答
2

选择不是基于“最佳匹配”,而只是满足该州最低标准的第一个项目。

http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

所以我认为它是透明的,因为其中一个状态与当前状态匹配

  <item android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/tranpsshape" /> 
  <item android:state_window_focused="false" android:state_enabled="false" android:drawable="@drawable/tranpsshape" />
  <item android:state_enabled="true" android:drawable="@drawable/tranpsshape" /> 
  <item android:state_focused="true" android:drawable="@drawable/tranpsshape" /> 

试着放

<item android:state_selected="true" android:drawable="@drawable/selectedshape" /> 

在顶部或将条件添加到以前的案例。

于 2012-09-07T15:23:36.083 回答