带文字的ImageButton[自定义]
来源:互联网 发布:如何用c语言表白 编辑:程序博客网 时间:2024/05/03 12:46
转载:http://www.cnblogs.com/sjrhero/articles/2601465.html
今天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.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 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
0 0
- 带文字的ImageButton[自定义]
- 带文字的ImageButton[自定义]
- android--自定义带文字的ImageButton类
- 带文字的ImageButton
- 自定义Android带图片和文字的ImageButton
- 【自定义Android带图片和文字的ImageButton】
- 自定义Android带图片和文字的ImageButton
- 自定义Android带图片和文字的ImageButton
- 【自定义Android带图片和文字的ImageButton】
- 【自定义Android带图片和文字的ImageButton】
- 实现带图片和文字的imagebutton
- Android带文字的ImageButton实现
- 轻松实现带文字的ImageButton
- Android带文字的ImageButton实现
- Android带文字的ImageButton实现
- Android带文字的ImageButton实现
- 轻松实现ImageButton带文字
- android imagebutton 带文字图片的实现方法
- 多项式求和求积(链表)
- 生活的反思
- PB程序优化
- 调整音频模型以实现更好的语音识别
- python实现自动发送微博,当自己写博客时同步上去。
- 带文字的ImageButton[自定义]
- 解析ANDROID ps命令执行后各项参数的含义 .
- 七种排序实现
- 差分约束系统总结
- 【LeetCode】Longest Consecutive Sequence
- 十张内涵图12月15日
- VB鼠标事件
- hadoop集群空间使用情况报告脚本
- Android应用开发以及设计思想深度剖析(5)