widgets之ImageButton

来源:互联网 发布:mantis迁移到linux下 编辑:程序博客网 时间:2024/05/19 02:27

1.ImageButton和普通的Button区别就在于外观更好看,可以用一个图片当做一个Button,当然也会有配套的UI设计方案。

声明方法和普通的Button差不多,只不过这里引用了另一个xml文件作为这个Button切换的效果布局

<ImageButton        android:id="@+id/ImageButton01"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@drawable/imagebutton01" />

最后一个标签使用了drawable中的一个imagebutton01.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/up"/><item android:state_focused="true"android:drawable="@drawable/down"/><item android:drawable="@drawable/normal"/></selector>

其中android:state_pressed="true"

表示当按钮压下去时,这个ImageButton应该是什么样的。

android:state_focused="true"
表示当按钮获取焦点的时候,这个ImageButton应该是什么样的。

但是如果只是设置成这样样子,在Activity中使用setOnFocusChangeListener是没有效果的,需要再加上点标签才可以:

android:focusable="true"android:focusableInTouchMode="true"
监听事件为:

ib1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubToast.makeText(MainActivity.this, "ImagButton被点击了", 3333).show();}});ib1.setOnFocusChangeListener(new OnFocusChangeListener() {@Overridepublic void onFocusChange(View arg0, boolean arg1) {// TODO Auto-generated method stubToast.makeText(MainActivity.this, "ImagButton获取了焦点", 3333).show();}});

补充:selector标签主要是用来改变ListView和Button控件的默认背景的,里面可以使用Item标签定义自己想要的样式。

引用这个xml的标签两者也有不同:

android:listSelector="@drawable/mylist_view" 

android:background="@drawable/mylist_view" 
listView以上两种方法都可以使用只不过第一个是在listView中,第二个是在Item中。但是Button只能使用第二种。






0 0