android自定义控件和自定义回调函数步骤示例
自定义控件的步骤:
1 view的工作原理
2 编写view类
3 为view类增加属性
4 绘制屏幕
5 响应用户消息
6 自定义回调函数
java代码
private class mytext extends linearlayout {
private textview text1;
/*
* private string text;
*
* public string gettext() { return text; }
*
* public void settext(string text) { this.text = text; }
*/
public mytext(context context) {
super(context);
// todo auto-generated constructor stub
layoutinflater inflate = (layoutinflater) context
.getsystemservice(context.layout_inflater_service);
view view = inflate.inflate(r.layout.tabhost_item, this, true);
text1 = (textview) view.findviewbyid(r.id.tabhost_tv);
}
public void settextviewtext(string tabhost_name) {
text1.settext(tabhost_name);
}
/*
* @override protected void ondraw(canvas canvas) { // todo
* auto-generated method stub super.ondraw(canvas); paint p = new
* paint(); p.setcolor(color.white); p.settextsize(10);
* canvas.drawtext(text, 25, 25, p); }
*/
}
xml代码
<?xml version="1.0" encoding="utf-8"?>
<!-- gmaptabactivity中自定义控件mytext的自布局 -->
<textview
android:id="@+id/tabhost_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
上一篇: 桂圆的功效与作用,你知道哪些呢?
下一篇: 孕妇喝什么水好,喝什么水不好?