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

时间:2020-12-18 23:44:39

To give users visual feedback when they touch a view, I do

为了在用户触摸视图时给予用户视觉反馈,我这样做

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

But ?attr/selectableItemBackground is a gray color. I want to use a different color. To do that I do

但是?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>

but it does not work, even after I set clickable="true" for the view in question.

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

1 个解决方案

#1


0  

Follow the code

遵循代码

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>

assign button_background.xml as background for the button by changing the colors which u desire. Hope it works!!!

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

#1


0  

Follow the code

遵循代码

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>

assign button_background.xml as background for the button by changing the colors which u desire. Hope it works!!!

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