带文字的ImageButton
来源:互联网 发布:做淘宝要怎么做 编辑:程序博客网 时间:2024/06/10 16:30
要实现带文字的ImageButton的方法很多,我这里仅列举一种方法:自定义一个继承自ImageButton的类,然后Override它的onDraw(Canvas canvas)方法。
public
class
MyImageButton
extends
ImageButton {
private
String text =
null
;
//要显示的文字
private
int
color;
//文字的颜色
public
MyImageButton(Context context, AttributeSet attrs) {
super
(context,attrs);
}
public
void
setText(String text){
this
.text = text;
//设置文字
}
public
void
setColor(
int
color){
this
.color = color;
//设置文字颜色
}
@Override
protected
void
onDraw(Canvas canvas) {
super
.onDraw(canvas);
Paint paint=
new
Paint();
paint.setTextAlign(Paint.Align.CENTER);
paint.setColor(color);
canvas.drawText(text,
15
,
20
, paint);
//绘制文字
}
}
下面进行测试,在布局文件中定义两个MyImageButton类型的控件button01和button02
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<LinearLayout
xmlns:android=
"http://schemas.android.com/apk/res/android"
android:orientation=
"horizontal"
android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
>
<com.alex.layout.MyImageButton
android:id=
"@+id/button01"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:background=
"@drawable/button_bg"
/>
<com.alex.layout.MyImageButton
android:id=
"@+id/button02"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:background=
"@drawable/button_bg"
/>
</LinearLayout>
最后在activity中分别设置button01和button02要显示的文字和文字的颜色
button01= (MyImageButton)findViewById(R.id.button01);
button01.setText(
"呵呵"
);
button01.setColor(Color.RED);
button02 = (MyImageButton)findViewById(R.id.button02);
button02.setColor(Color.BLUE);
button02.setText(
"哈哈"
);
- 带文字的ImageButton
- 带文字的ImageButton[自定义]
- 带文字的ImageButton[自定义]
- 实现带图片和文字的imagebutton
- Android带文字的ImageButton实现
- 轻松实现带文字的ImageButton
- Android带文字的ImageButton实现
- android--自定义带文字的ImageButton类
- Android带文字的ImageButton实现
- Android带文字的ImageButton实现
- 轻松实现ImageButton带文字
- 自定义Android带图片和文字的ImageButton
- 【自定义Android带图片和文字的ImageButton】
- android imagebutton 带文字图片的实现方法
- 自定义Android带图片和文字的ImageButton
- 自定义Android带图片和文字的ImageButton
- 【自定义Android带图片和文字的ImageButton】
- 【自定义Android带图片和文字的ImageButton】
- C#中Application类简述
- MySQL 1364 错误提示:#1364 - Field "XX" doesn't have a def...
- Nginx rewrite 配置问号解决办法
- 去掉NSString空字符串
- oracle 的pdm和dmp含义
- 带文字的ImageButton
- 创业公司老板管理常识
- MC新手入门(四十七)------ 重力感应
- mysql in 子查询 效率慢 优化(转)
- MySQL 错误 1366:1366 Incorrect integer valueMySQL 错误 1366:1366 Incorrect integer value
- oracle查询某表中是否含有某字段
- QT环境的配置和使用
- zxing 竖屏 图像旋转90度 解决方法
- Android网络连接判断与处理