今天Android項目中遇到一個ImageButton控件上面要顯示文字,無奈自定義了一個ImageButton,繼承自ImageButton。其實就是override這個控件的onDraw(Canvas canvas)方法;代碼如下:
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; /** * 自定義ImageButton * 可以在ImageButton上面設置文字 * @author SJR */ public class CustomImageButton extends ImageButton { private String _text = ""; private int _color = 0; private float _textsize = 0f; public CustomImageButton(Context context, AttributeSet attrs) { super(context, attrs); } public void setText(String text){ this._text = text; } public void setColor(int color){ this._color = color; } public void setTextSize(float textsize){ this._textsize = textsize; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint paint = new Paint(); paint.setTextAlign(Align.CENTER); paint.setColor(_color); paint.setTextSize(_textsize); canvas.drawText(_text, canvas.getWidth()/2, (canvas.getHeight()/2)+12, paint); } }
Activity對應的布局xml文件代碼:
<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>
這是我的程序里面使用這個自定義ImageButton的代碼。
下面就是為這個自定義的ImageButton設置字體了(就是我使用這個自定義的ImageButton的Activity里面),代碼如下:
package sRoger.pack.MyActivity import 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 class LoginActivity extends Activity { private CustomImageButton login_btn_login; private CustomImageButton login_btn_register; //自定義ImageButton上面顯示的字體的大小 private float BTN_TEXTSIZE = 32f; //自定義ImageButton上面顯示的字體的顏色 private int BTN_TEXTCOLOR = Color.WHITE; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 設置當前Activity沒有標題欄 requestWindowFeature(Window.FEATURE_NO_TITLE); //設置當前Activity為全屏模式 getWindow().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); // 設置自定義ImageButton上面要顯示的文本內容 login_btn_login.setText("登錄"); // 設置自定義ImageButton上面要顯示的文本內容 login_btn_register.setText("注冊"); login_btn_login.setColor(BTN_TEXTCOLOR); login_btn_register.setColor(BTN_TEXTCOLOR); login_btn_login.setTextSize(BTN_TEXTSIZE); login_btn_register.setTextSize(BTN_TEXTSIZE); } }
自定義其它控件也與之類似,比如實現控件的圓角等。。。
參考:http://www.cnblogs.com/and_he/archive/2011/10/13/2210257.html
http://www.cnblogs.com/zchajax/archive/2011/05/02/2034453.html
