Display an image button that can be clicked by users. By default, imagebutton looks like a normal button and changes the background color in different States (such as pressing. <Imagebutton>
Android: src attribute of the XML element or setimageresource (INT) method.
To indicate different button states (Focus, selection, etc.), you can define different images for various States. For example, define a blue image as the default image, a yellow image as the image displayed when the current focal point is obtained, and a yellow image as the image displayed when the button is pressed. This can be done in a simple way-the XML "selector." configuration is as follows:
Save the above XMLres/drawable/Folder (Note: The file name is case sensitive !), Set the file name as a parameter to
Android: src attribute (Note: for example, if the XML file name is myselector. XML, set this attribute to "@ drawable/myselector" and set Android: background as well, but the effect is not the same ). Android will automatically search for the corresponding image in XML for Display Based on the button status change.
<Item> the order of elements is very important, because it is used to determine whether it is applicable to the current button status, which is why the specified image in the normal (default) status is placed at the end, it will only be used after both pressed and focused fail to judge. (Note: for example, when a button is pressed, the focus is obtained at the same time, but the focus is not necessarily pressed. Therefore, we will search for the focus in order. If the focus is found properly, we will not look down. If the button is clicked, the first one will be selected and will not be found later .)
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="ImageButton"/> <ImageButton android:id="@+id/image1" android:layout_width= "wrap_content" android:layout_height="wrap_content" android:src= "@drawable/macos01"/></LinearLayout>
After clicking