欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android自定义控件和自定义回调函数步骤示例

程序员文章站 2023-11-14 08:29:40
自定义控件的步骤: 1 view的工作原理2 编写view类3 为view类增加属性4 绘制屏幕5 响应用户消息6 自定义回调函数 java代码 复制代码 代码如下:...

自定义控件的步骤:

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"
     />