ImageButton

ImageButton displays a button with an image (instead of text) that can be pressed or clicked by the user. By default, an ImageButton looks like a regular Button, with the standard button background that changes color during different button states. The image on the surface of the button is defined either by the android:src attribute in the XML element or by the setImageResource(int) method.

To remove the standard button background image, define your own background image or set the background color to be transparent.

ImageButton

To implement a ImageButton, create three images (for normal, on focus, ad clicked) and place in /drawable folder.
<?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/imgbutton_3" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/imgbutton_2" /> <!-- focused -->
<item android:drawable="@drawable/imgbutton_1" /> <!-- default -->
</selector>


Create a xml in /drawable folder, named imgbutton.xml here.

Add ImageButton in XML, refer to imgbutton.xml as android:background.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ImageButton
android:layout_width="100dp"
android:layout_height="wrap_content"
/>
<ImageButton
android:id="@+id/myimagebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/imgbutton"
/>
</LinearLayout>


0 Response to "ImageButton"

Posting Komentar