Android Development Note ___ Image button __imagebutton

Source: Internet
Author: User

IMAGEBUTTON

It is actually derived from the image view, not derived from the button. , image view has properties and methods that are owned by the image button, except that ImageButton has a default button appearance.

    • Image button can only display graphics
    • ImageButton the picture above can be stretched proportionally
    • Only one graphic can be displayed in the background, but two pictures are displayed in the foreground and background, and the effect of picture overlay is achieved.

    • In the input method cannot enter the character and the special font display string, is suitable uses ImageButton, first transduction again displays

    • To place a picture around text, you can use a button that is based on the text view,
    • The following properties are set specifically in XML:
      • Drawabletop: Specify a picture above the text
      • Drawablebottom: Specify a picture below the text
      • Drawableleft: Specifies the graphic to the left of the text
      • Drawableright: Specifies the picture to the right of the text
      • Drawablepadding: Specify graphic text spacing
    • In the code:
      • Setcompounddrawables: Set the text around the graphic, up or down
      • Setcompounddrawablepadding: Pitch

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "vertical">6 7     <Button8         Android:id= "@+id/btn_icon"9 Android:layout_width= "Wrap_content"Ten Android:layout_height= "Wrap_content" One Android:layout_margintop= "10DP" A android:layout_gravity= "Center" - Android:drawableleft= "@mipmap/ic_launcher" - android:drawablepadding= "5DP" the Android:text= "Warm welcome" - Android:textcolor= "#000000" - android:textsize= "17SP" /> -  +     <LinearLayout -         Android:layout_width= "Match_parent" + Android:layout_height= "Wrap_content" A Android:layout_margintop= "10DP" at android:orientation= "Horizontal"> -  -         <Button -             Android:id= "@+id/btn_left" - Android:layout_width= "0DP" - Android:layout_height= "Wrap_content" in Android:layout_weight= "1" - Android:text= "icon in left" to Android:textcolor= "#000000" + android:textsize= "15SP" /> -  the         <Button *             Android:id= "@+id/btn_top" $ Android:layout_width= "0DP"Panax Notoginseng Android:layout_height= "Wrap_content" - Android:layout_weight= "1" the Android:text= "icon on top" + Android:textcolor= "#000000" A android:textsize= "15SP" /> the  +         <Button -             Android:id= "@+id/btn_right" $ Android:layout_width= "0DP" $ Android:layout_height= "Wrap_content" - Android:layout_weight= "1" - Android:text= "icon in right" the Android:textcolor= "#000000" - android:textsize= "15SP" />Wuyi  the         <Button -             Android:id= "@+id/btn_bottom" Wu Android:layout_width= "0DP" - Android:layout_height= "Wrap_content" About Android:layout_weight= "1" $ Android:text= "icon in the bottom" - Android:textcolor= "#000000" - android:textsize= "15SP" /> -  A     </LinearLayout> +  the </LinearLayout>

Java

1  PackageCom.example.alimjan.hello_world;2 3 ImportAndroid.content.Context;4 Importandroid.content.Intent;5 Importandroid.graphics.drawable.Drawable;6 ImportAndroid.os.Bundle;7 Importandroid.support.v7.app.AppCompatActivity;8 ImportAndroid.view.View;9 ImportAndroid.widget.Button;Ten  One /** A * Created by Alimjan on 7/1/2017. -  */ -  the  -  Public classClass__2_3_4extendsAppcompatactivityImplementsView.onclicklistener { -     PrivateButton Btn_icon; -     Privatedrawable drawable; +  - @Override +     protected voidonCreate (Bundle savedinstancestate) { A         Super. OnCreate (savedinstancestate); at Setcontentview (r.layout.code_2_3_4); -Btn_icon =(Button) Findviewbyid (R.id.btn_icon); -drawable =getresources (). getdrawable (R.mipmap.ic_launcher); -         //the picture size must be set or the picture will not be displayed -Drawable.setbounds (0, 0, Drawable.getminimumwidth (), Drawable.getminimumheight ()); -Button Btn_left =(Button) Findviewbyid (r.id.btn_left); inButton Btn_top =(Button) Findviewbyid (r.id.btn_top); -Button btn_right =(Button) Findviewbyid (r.id.btn_right); toButton Btn_bottom =(Button) Findviewbyid (r.id.btn_bottom); +Btn_left.setonclicklistener ( This); -Btn_top.setonclicklistener ( This); theBtn_right.setonclicklistener ( This); *Btn_bottom.setonclicklistener ( This); $     }Panax Notoginseng  - @Override the      Public voidOnClick (View v) { +         if(V.getid () = =r.id.btn_left) { ABtn_icon.setcompounddrawables (drawable,NULL,NULL,NULL); the}Else if(V.getid () = =r.id.btn_top) { +Btn_icon.setcompounddrawables (NULL, Drawable,NULL,NULL); -}Else if(V.getid () = =r.id.btn_right) { $Btn_icon.setcompounddrawables (NULL,NULL, Drawable,NULL); $}Else if(V.getid () = =R.id.btn_bottom) { -Btn_icon.setcompounddrawables (NULL,NULL,NULL, drawable); -         } the     } - Wuyi      Public Static voidstarthome (Context mcontext) { theIntent Intent =NewIntent (Mcontext, Class__2_3_4.class); - mcontext.startactivity (intent); Wu     } -}

Android Development Note ___ Image button __imagebutton

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.