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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2021-03-19 我要评论
想了解android自定义控件和自定义回调函数步骤示例的相关内容吗,在本文为您仔细讲解android自定义控件的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:回调函数,自定义控件,下面大家一起来学习吧。

自定义控件的步骤:

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们