自定义控件-初次相见,请多多指教

前言:

为什么要使用自定义控件呢?好吧:我老实交代我初次使用自定义控件的原因。大学嘛,初生牛犊不怕虎,就跑去接了项目。一开始做那些简单的页面的时候,贼开心了。敲敲敲,终于遇到比较麻烦的事情了。所以嘛,我现在回想了一下可能也有像我一样的新手也会遇到这个问题,所以我就把我的第一次初始化控件的过程分享给大家。


创建过程:

  •    考虑清楚你的自定义控件需要(如固定功能栏 就是 图片 加上 文字)     
  •    在attr文件中添加属性
  •    开始编写自定义控件
  •     在xml文件中进行引用         


开始我的小demo创建  :

第一步:思考我需要什么

            我当时的功能需求就是 每当有新消息来到时就会出现如下图的小红点 

            然后为什么会使用到组合控件呢 

            第一点 我懒 好多重复的代码 我烦     第二点嘛 就是 想想学习新东西也不错嘛 哈哈哈

                      

        然后 经过思考 图片 文字 还有 红点是否显示  就是三个突出点不同点


第二步:在attr中添加属性 

            下方的属性值命名会在我们的第三步骤和第四步骤得到体现

    <declare-styleable name="WorkbenchContent">
        <attr name="wc_new_notice" format="boolean" />
        <attr name="wc_image" format="reference" />
        <attr name="wc_content" format="string" />
   </declare-styleable>


第三步:开始编写我们的自定义控件类 WorkbenchContent

             在这之前我们先来制作我们的item view文件 (view_workbench)                                

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/wc_image"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_centerHorizontal="true"
        android:background="@drawable/workbench_ic_company_announcements" />

    <TextView
        android:id="@+id/wc_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/wc_image"
        android:layout_centerHorizontal="true"
        android:text="公司公告"
        android:textColor="#5D5F6A"
        android:textSize="14dp" />

    <TextView
        android:id="@+id/wc_new_notice"
        android:layout_width="10dp"
        android:layout_height="10dp"
        android:layout_alignRight="@+id/wc_content"
        android:layout_marginTop="5dp"
        android:background="@drawable/common_bd_red" />
</RelativeLayout>

效果如下:

                   

好啦 准备工作就绪现在就开始我们的征途

    1、我让类继承了 RelativeLayout 开始完成它的三个构造方法 ,并最终实现了他的第三个方法。

    public WorkbenchContent(Context context) {
        this(context, null);
    }

    public WorkbenchContent(Context context, AttributeSet as) {
        this(context, as, 0);
    }

    public WorkbenchContent(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        myview = LayoutInflater.from(context).inflate(R.layout.view_workbench, this, true);
        if (attrs != null)
            initAttrs(context, attrs, defStyleAttr);
        initViews();
    }

    2、为我们的控件获得attrs中的各个属性资源

    public void initAttrs(Context context, AttributeSet as, int def) {
        TypedArray ta = context.obtainStyledAttributes(as, R.styleable.WorkbenchContent, def, 0);
        wc_image = ta.getDrawable(R.styleable.WorkbenchContent_wc_image);
        wc_content = ta.getString(R.styleable.WorkbenchContent_wc_content);
        wc_new_notice = ta.getBoolean(R.styleable.WorkbenchContent_wc_new_notice, false);
    }
obtainStyledAttributes 方法为 我们的资源容器获得各个属性值
TypedArray 我们可以通过这个容器检索到我们想要的资源,就是通过检索res资源中结构的特定值索引到对应的资源。

  3、开始我们的小需求

        实现当有新消息来到就是出现红色圈 消失就隐藏红色圈。

 public void initViews() {
        tv_content = (TextView) myview.findViewById(R.id.wc_content);
        tv_news = (TextView) myview.findViewById(R.id.wc_new_notice);
        iv_photos =  (ImageView) myview.findViewById(R.id.wc_image);
        tv_content.setText(wc_content);
        if (wc_new_notice ) {
            tv_news.setVisibility(VISIBLE);
        } else {
            tv_news.setVisibility(GONE);
        }
    }


第二步:最后一个步骤就是在xml文件中进行引用 

<com.handsome.projectnz.CustomView.WorkbenchContent
android:id="@+id/iv_working_task"
android:layout_width="wrap_content"
app:wc_content="工作任务"
android:layout_height="wrap_content"
app:wc_new_notice="true
app:wc_image="@drawable/workbench_ic_working_task"" />
到这里我相信接下来的其他业务大家都有办法自进行解决了,到这里我也就完成了我的工作,敬请期待下集。





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值