Android Button 按钮 设置 各种状态 图片 颜色
2014-04-18 10:56
525 查看
有2个方法可以实现,一种是用 选择器 定义每种状态的图片 selec.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/text_down"
></item>
<item
android:drawable="@drawable/append1"
></item>
</selector>
:
在xml里定义按钮的背景色android:background="@drawable/ selec " 或在代码中也可以动态设置 btncall.setBackgroundResource(R.drawable. selec );
另一种就是在触摸事件中直接设置
private
Button mBtn; //定义我们的按钮
在onCreate中加入
mBtn
= (Button) findViewById(R.id.btn); //btn为layout中的Button ID
mBtn.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View arg0,MotionEvent arg1)
{
if(arg1.getAction() == MotionEvent.ACTION_DOWN)
{
arg0.setBackgroundResource(R.drawable.pressed);
//按下的图片对应pressed
}
else if(arg1.getAction() == MotionEvent.ACTION_UP)
{
arg0.setBackgroundResource(R.drawable.normal);
//常态下的图片对应normal
}
else
if() //这里还可以继续实现MotionEvent.ACTION_MOVE和MotionEvent.ACTION_CANCEL等实现更多的特效
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:state_pressed="true"
android:drawable="@drawable/text_down"
></item>
<item
android:drawable="@drawable/append1"
></item>
</selector>
:
在xml里定义按钮的背景色android:background="@drawable/ selec " 或在代码中也可以动态设置 btncall.setBackgroundResource(R.drawable. selec );
另一种就是在触摸事件中直接设置
private
Button mBtn; //定义我们的按钮
在onCreate中加入
mBtn
= (Button) findViewById(R.id.btn); //btn为layout中的Button ID
mBtn.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View arg0,MotionEvent arg1)
{
if(arg1.getAction() == MotionEvent.ACTION_DOWN)
{
arg0.setBackgroundResource(R.drawable.pressed);
//按下的图片对应pressed
}
else if(arg1.getAction() == MotionEvent.ACTION_UP)
{
arg0.setBackgroundResource(R.drawable.normal);
//常态下的图片对应normal
}
else
if() //这里还可以继续实现MotionEvent.ACTION_MOVE和MotionEvent.ACTION_CANCEL等实现更多的特效
相关文章推荐
- android 设置 button 不同状态的图片
- Android button设置左边图片离按钮左边的距离,离文本的距离
- Android设置Selector不同状态下颜色及图片
- 在Android使用XML文件控制按钮文字在各种状态下的颜色
- Android——图片视图(ImageView)、状态开关按钮(ToggleButton)、时钟、图片透明度、滚动和时间选择器
- android中用xml文件设置button的各个状态的图片。
- Android selector的用法及设置按钮不同状态下的背景图片
- Android设置Selector不同状态下颜色及图片
- Android之自定义背景Button按钮、自定义形状Button的全攻略、设置字体颜色
- Android设置Button各种状态下背景改变
- Android设置Selector不同状态下颜色及图片
- Android设置Selector不同状态下颜色及图片
- Android设置Selector不同状态下颜色及图片
- iOS小问题---设置TabBarItem的字体大小、颜色,各种状态下的图片
- 设置button不同状态下的背景色,即把这个颜色变成图片设置成,背景图片
- Android——滚动视图(ScrollView)图片视图(ImageView)、状态开关按钮(ToggleButton)、时钟
- android 读取sd卡(sdcard)图片设置为按钮背景(Button背景设置)代码
- Android利用selector设置按钮不同状态下的背景图片
- Button按钮状态背景的设置
- Android开源项目——设置图文居中的按钮 IconButton