自定义View

前言

本篇文章主要讲解的有关Android开发中常用的自定义View实现TitleBar的处理方式以及应用。


文章目录

  • 前言
    • @[TOC](文章目录)
  • 一、初始化
    • 1.初始化view布局
    • 2.获取xml文件中的自定义属性并赋值
  • 二、设置方法调用
    • 1.设置左侧文本、图片监听
    • 2.设置左侧图片背景
    • 3.设置左侧文本内容
    • 4.设置左侧文本颜色
    • 5.设置左侧图片是否可见
    • 6.设置底部线是否可见
    • 7.设置左侧文本是否可见
    • 8.设置右侧属性
    • 9.设置中间属性
    • 10.对应的布局和属性
    • 11.对应的styleable属性
  • 附上全部代码
  • 总结

下面主要讲解具体的使用方法

一、初始化

1.初始化view布局

fun init(context : Context?){
   
        val view = LayoutInflater.from(context).inflate(R.layout.custom_top_title_bar_white,this,true)
        bgRll = view.findViewById(R.id.bg_rll)
        imgLeft = view.findViewById(R.id.img_left)
        imgRight = view.findViewById(R.id.img_right)
        tvLeft = view.findViewById(R.id.tv_left)
        tvRight = view.findViewById(R.id.tv_right)
        tvCenter = view.findViewById(R.id.tv_title_center)
        titleLine = view.findViewById(R.id.title_line)
    }

2.获取xml文件中的自定义属性并赋值

val typedArray : TypedArray = context.obtainStyledAttributes(attrs,R.styleable.CustomTopTitleBar)
        //设置背景色
        val titleBack = typedArray.getColor(R.styleable.CustomTopTitleBar_ctb_back, Color.parseColor("#FFFFFF"))
        bgRll.setBackgroundColor(titleBack)
        
        //设置底部分割线(默认不显示)
        val isShowLine = typedArray.getBoolean(R.styleable.CustomTopTitleBar_ctb_show_line,true)
        titleLine.visibility = if (isShowLine) View.GONE else View.VISIBLE

        //设置标题内容
        val strTitle = typedArray.getString(R.styleable.CustomTopTitleBar_ctb_title)
        tvCenter.text = strTitle ?: resources.getString(R.string.app_name)
         //左侧
         //设置左边图片(默认显示)
         //隐藏-false 显示-true
        val isShowLeftImg = typedArray.getBoolean(R.styleable.CustomTopTitleBar_ctb_show_img_left,true)
        imgLeft.visibility = if(isShowLeftImg) View.VISIBLE else View.GONE

        //设置左侧文本(默认隐藏)
        //隐藏-false 显示-true
        val isShowLeftTv = typedArray.getBoolean(R.styleable.CustomTopTitleBar_ctb_show_text_left,false)
        tvLeft.visibility = if (isShowLeftTv) View.VISIBLE else View.GONE

        //设置左侧图片背景
        val drawableLeft = typedArray.getDrawable(R.styleable.CustomTopTitleBar_ctb_img_left)
        if(drawableLeft != null){
   
            imgLeft.setImageDrawable(drawableLeft)
        }

        //设置左侧文本内容
        val strLeft = typedArray.getString(R.styleable.CustomTopTitleBar_ctb_text_left)
        if(strLeft.isNullOrBlank()){
   
            tvLeft.text = resources.getString(R.string.def_back_left_con)
        }else{
   
            tvLeft.text = strLeft
        }
//右侧
        //设置右侧图片(默认隐藏)
        //隐藏-false 显示-true
        val isShowRightImg = typedArray.getBoolean(R.styleable.CustomTopTitleBar_ctb_show_img_right,false)
        imgRight.visibility = if (isShowRightImg) View.VISIBLE else View.GONE

        //设置右侧文本(默认隐藏)
        //隐藏-false 显示-true
        val isShowRightTv = typedArray.getBoolean(R.styleable.CustomTopTitleBar_ctb_show_text_right,false)
        tvRight.visibility = if (isShowRightTv) View.VISIBLE else View.GONE

        //设置右侧图片背景
        val drawableRight = typedArray.getDrawable(R.styleable.CustomTopTitleBar_ctb_img_right)
        if(drawableRight != null){
   
            imgRight.setImageDrawable(drawableRight)
        }

        //设置右侧文本内容
        val strRight = typedArray.getString(R.styleable.CustomTopTitleBar_ctb_text_right)
        if(strRight.isNullOrBlank()){
   
            tvRight.text = resource

你可能感兴趣的:(自定义控件基础,Kotlin,opencv,人工智能,计算机视觉)