问题描述
- android 点击更换Button背景出现重叠
-
Buttonandroid:src=" android:src="@drawable/voice"(A)
点击时A一直存在,怎么办,写了search_voice.setBackgroundResource(0);不起作用
voice.xml
voice_press.xmlpublic void onClick(View arg0) { search_voice.setBackgroundResource(0); if(flag){ Toast.makeText(getApplicationContext(), "语音开启",Toast.LENGTH_SHORT).show(); search_voice.setBackgroundResource(R.drawable.vocie_press); flag = false; } else{ Toast.makeText(getApplicationContext(), "语音屏蔽",Toast.LENGTH_SHORT).show(); search_voice.setBackgroundResource(R.drawable.vocie); flag = true; } } });
解决方案
你默认先别设置背景呢,你这代码没啥问题,或者你用drawlable的xml来设置各种状态下的效果,比如下面的三态效果
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- unpressed -->
<item android:state_pressed="false" android:state_enabled="false" android:color="@color/white">
<shape>
<solid android:color="#bfbfbf"/>
<corners android:radius="5dp"/>
</shape>
</item>
<item android:state_pressed="false" android:state_enabled="true" android:color="@color/white">
<shape>
<solid android:color="#23a8f0"/>
<corners android:radius="5dp"/>
</shape>
</item>
<!-- pressed -->
<item android:state_pressed="true" android:state_enabled="true" android:color="@color/white">
<shape>
<solid android:color="#176b99"/>
<corners android:radius="5dp"/>
</shape>
</item>
</selector>
时间: 2024-10-17 08:52:55