添加自定义颜色以触摸android中的反馈

时间:2015-10-20 21:07:41

标签: android android-layout touch

为了在用户触摸视图时为其提供视觉反馈,我

<View
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="?attr/selectableItemBackground" />

但是?attr/selectableItemBackground是灰色的。我想用不同的颜色。要做我做

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="schemas.android.com/apk/res/android">;
    <item android:drawable="@color/mine" android:state_selected="true"></item>
    <item android:drawable="@color/mine" android:state_pressed="true"></item>
    <item android:drawable="@android:color/transparent"></item>
</selector>

但即使我为相关视图设置了clickable="true",它也无效。

1 个答案:

答案 0 :(得分:0)

按照代码

button_normal.xml

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

   <solid android:color="@color/button_light_green"/>
    <corners android:radius="5dp" />

</shape>

button_selected.xml

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

   <solid android:color="@color/button_light_green_selected"/>
    <corners android:radius="5dp" />

</shape>

button_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@drawable/button_selected"/>
<item android:state_focused="true" android:drawable="@drawable/button_selected"/>
<item android:drawable="@drawable/button_normal"/> 
</selector>

通过更改您想要的颜色,将button_background.xml指定为按钮的背景。希望它有效!!!