当前位置: 移动技术网 > IT编程>移动开发>Android > android自定义控件和自定义回调函数步骤示例

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

向井蓝18才,上海挂号网上预约,包若兮

自定义控件的步骤:

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网