android之ImageButton实现选中改变背景,附加(图片自动适配组件大小)

1.如下是ImageButton
<ImageButton android:id="@+id/jmxw" android:layout_width="fill_parent" android:layout_marginLeft="4dip" android:layout_height="0dip" android:layout_weight="784" android:background="@drawable/menupagexw"/>


 

2.在androidd项目的drawable下添加"android xml file"资源,选择类型为"selector",文件名为"menupagexw"

 

3.在menupagexw.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/jmxw_hl"/>
    <item android:state_pressed="false" android:drawable="@drawable/jmxw"/>
</selector>


 

这样这个按钮在选中不和选中的情况下,背景就不一样了。

 

附加:

android:src=“图片id”,这时候图片不会根据组件大小而调整,但是android:background="图片id",会自动调整图片显示比列,来适配组件

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android 单击选中ImageButton[]图像数组用法示例,类似于Radio的功能,不过用图片表现,这样可使Android UI更加友好,视觉更漂亮一些,用户轻触图片,即可选中该数据项,比Radio用户体验更好,如截图所示的选中效果,选中后图像会变色:   setContentView(R.layout.main);   myTextView = (TextView) this.findViewById(R.id.myTextView);//得到myTextView的引用   imageButtons[0] = (ImageButton) this.findViewById(R.id.button01);//得到button01的引用   imageButtons[1] = (ImageButton) this.findViewById(R.id.button02);//得到button02的引用   imageButtons[2] = (ImageButton) this.findViewById(R.id.button03);//得到button03的引用   imageButtons[3] = (ImageButton) this.findViewById(R.id.button04);//得到button04的引用   for(ImageButton imageButton : imageButtons){   imageButton.setOnFocusChangeListener(this);//添加监听   我们同时使用了if else来判断各个ImageButton数组元素的选中状态:   实现了接口中的方法   if(v.getId() == R.id.button01){//改变的是button01时    myTextView.setText("您选中了羊!");   }else if(v.getId() == R.id.button02){//改变的是button02时    myTextView.setText("您选中了猪!");   }else if(v.getId() == R.id.button03){//改变的是button03时    myTextView.setText("您选中了牛!");   }else if(v.getId() == R.id.button04){//改变的是button04时    myTextView.setText("您选中了鼠!");   }else{//其他情况    myTextView.setText("");   }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北京橙溪科技有限公司enwing.com

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值