Today the Android project encountered a ImageButton control above to display the text, helpless custom a ImageButton, inherited from ImageButton. This is actually the OnDraw (canvas Canvas) method for the override of this control, as follows:
Package Sroger.pack.utility;import Android.content.context;import android.graphics.canvas;import Android.graphics.paint;import Android.graphics.paint.align;import Android.util.attributeset;import Android.widget.ImageButton;/** * Custom ImageButton * can set text on ImageButton * @author SJR*/ Public classCustomimagebutton extends ImageButton {PrivateString _text =""; Private int_color =0; Private float_textsize =0f; PublicCustomimagebutton (Context context, AttributeSet Attrs) {Super (context, attrs); } Public voidSetText (String text) { This. _text =text; } Public voidSetColor (intcolor) { This. _color =color; } Public voidSettextsize (floattextsize) { This. _textsize =textsize; } @Overrideprotected voidOnDraw (canvas canvas) {super.ondraw (canvas); Paint Paint=NewPaint (); Paint.settextalign (Align.center); Paint.setcolor (_color); Paint.settextsize (_textsize); Canvas.drawtext (_text, Canvas.getwidth ()/2, (Canvas.getheight ()/2)+ A, paint); }}
Activity corresponds to the layout XML file code:
<LinearLayout android:layout_width="fill_parent"Android:layout_height="wrap_content"android:orientation="Horizontal"> <SRoger.pack.Utility.CustomImageButton Android:id="@+id/login_btnlogin"Android:layout_width="0DP"Android:layout_height="wrap_content"android:src="@drawable/LOGIN_BTN_BG"Android:background="#eeeeee"Android:scaletype="Fitxy"Android:layout_marginleft="15DP"Android:layout_margintop="15DP"Android:layout_marginright="5DP"Android:layout_weight="1"/> <SRoger.pack.Utility.CustomImageButton Android:id="@+id/login_btnregister"Android:layout_width="0DP"Android:layout_height="wrap_content"android:src="@drawable/LOGIN_BTN_BG"Android:background="#eeeeee"Android:scaletype="Fitxy"Android:layout_marginleft="5DP"Android:layout_marginright="15DP"Android:layout_margintop="15DP"Android:layout_weight="1"/> </LinearLayout>
This is the code inside my program that uses this custom ImageButton.
Here's how to set the font for this custom ImageButton (I'm using this custom ImageButton activity), the code is as follows:
Package SRoger.pack.MyActivityimport Sroger.pack.r;import sroger.pack.utility.customimagebutton;import Android.app.activity;import Android.graphics.color;import Android.os.bundle;import Android.util.Log;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.window;import Android.widget.linearlayout;import Android.widget.Toast; Public classLoginactivity extends Activity {PrivateCustomimagebutton Login_btn_login; PrivateCustomimagebutton Login_btn_register; //Custom ImageButton The size of the font shown above Private floatBtn_textsize =32f; //custom ImageButton The color of the font shown above Private intBtn_textcolor =Color.White; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); //set Current activity without title barrequestwindowfeature (Window.feature_no_title); //set Current Activity to full screen modeGetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN ); Setcontentview (r.layout.login_activity); Login_btn_login=(Customimagebutton) Findviewbyid (R.id.login_btnlogin); Login_btn_register=(Customimagebutton) Findviewbyid (r.id.login_btnregister); //set the text content to be displayed above the custom ImageButtonLogin_btn_login.settext ("Login"); //set the text content to be displayed above the custom ImageButtonLogin_btn_register.settext ("Register"); Login_btn_login.setcolor (Btn_textcolor); Login_btn_register.setcolor (Btn_textcolor); Login_btn_login.settextsize (btn_textsize); Login_btn_register.settextsize (btn_textsize); }}
imagebutton[custom with text]