Android 最简单的自定义MenuItem之一

作者: Jooyer, 时间: 2018.10.08

Github地址,欢迎点赞,fork

其实本篇和上一篇 Toolbar 是一起的,因为很多时候我们会自定义一个左边图片文本,右边文本图片的MenuItem.

本次代码也是比较简单,一个组合控件而已,我就直接贴源码,如果还有不清楚的,可以留言或者看github

###只需要一个类 + 一个 XML 文件即可,即拷即用

接下来我们依次讲解,和上一次Toolbar套路一致哈:
  1. CustomMenu
  2. 一个 XML 文件
  3. 属性及默认值

####首先,看看 CustomMenu

package cn.molue.jooyer.custommenu

import android.content.Context
import android.support.v4.content.ContextCompat
import android.text.TextUtils
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView

/**
 * Desc: 自定义普通菜单,格式如下
 *  图片 -- 文字  --------------- 文字/图片  图片
 * Author: Jooyer
 * Date: 2018-08-08
 * Time: 14:44
 */
class CustomMenu(context: Context, attr: AttributeSet, defStyleAttr: Int)
    : RelativeLayout(context, attr, defStyleAttr) {

    /**
     * 最左侧图标
     */
    private lateinit var iv_left_icon_menu: ImageView
    /**
     * 紧挨着左侧图标的文本
     */
    private lateinit var tv_left_name_menu: TextView
    /**
     * 紧挨着右侧的文本(与 紧挨着右侧的图片 只显示一种)
     */
    private lateinit var tv_right_name_menu: TextView
    /**
     * 紧挨着右侧的图片(与 紧挨着右侧的文本 只显示一种)
     */
    private lateinit var iv_near_right_icon_menu: ImageView
    /**
     * 最右侧图标(一般是向右箭头 →)
     */
    private lateinit var iv_right_arrow_menu: ImageView
    /**
     * 底部分割线
     */
    private lateinit var view_bottom_divider_menu: View

    private var rightTextRightMargin = 0

    constructor(context: Context, attr: AttributeSet) : this(context, attr, 0)

    init {
        initView()
        parseAttrs(context, attr)

    }

    private fun initView() {
        val parent = LayoutInflater.from(context).inflate( R.layout.menu_image_text_text_image, this,true)
        iv_left_icon_menu = parent.findViewById(R.id.iv_left_icon_menu)
        tv_left_name_menu = parent.findViewById(R.id.tv_left_name_menu)
        tv_right_name_menu = parent.findViewById(R.id.tv_right_name_menu)
        iv_near_right_icon_menu = parent.findViewById(R.id.iv_right_icon_menu)
        iv_right_arrow_menu = parent.findViewById(R.id.iv_right_arrow_menu)
        view_bottom_divider_menu = parent.findViewById(R.id.view_bottom_divider_menu)
    }

    private fun parseAttrs(context: Context, attr: AttributeSet) {
        val arr = context.obtainStyledAttributes(attr, R.styleable.CustomMenu)
        val leftImageVisible = arr.getBoolean(R.styleable.CustomMenu_cm_left_image_visible, true)
        val leftImageDrawable = arr.getDrawable(R.styleable.CustomMenu_cm_left_image_drawable)
        val leftImageWidth = arr.getDimension(R.styleable.CustomMenu_cm_left_image_width, dp2px(20F)).toInt()
        val leftImageHeight = arr.getDimension(R.styleable.CustomMenu_cm_left_image_height, dp2px(20F)).toInt()

        val leftTextInfo = arr.getText(R.styleable.CustomMenu_cm_left_text_info)
        val leftTextSize = arr.getInteger(R.styleable.CustomMenu_cm_left_text_size, 14).toFloat()
        val leftTextLeftMargin = arr.getDimension(R.styleable.CustomMenu_cm_left_text_left_margin, dp2px(10F)).toInt()
        val leftTextColor = arr.getColor(R.styleable.CustomMenu_cm_left_text_color,
                ContextCompat.getColor(context, R.color.color_111111))

        val rightTextInfo = arr.getText(R.styleable.CustomMenu_cm_right_text_info)
        val rightTextVisible = arr.getBoolean(R.styleable.CustomMenu_cm_right_text_visible, true)
        val rightTextSize = arr.getInt(R.styleable.CustomMenu_cm_right_text_size, 14).toFloat()
        rightTextRightMargin = arr.getDimension(R.styleable.CustomMenu_cm_right_text_right_margin, dp2px(20F)).toInt()
        val rightTextColor = arr.getColor(R.styleable.CustomMenu_cm_right_text_color,
                ContextCompat.getColor(context, R.color.color_111111))

        val rightNearImageVisible = arr.getBoolean(R.styleable.CustomMenu_cm_right_near_image_visible, false)
        val rightNearImageDrawable = arr.getDrawable(R.styleable.CustomMenu_cm_right_near_image_drawable)
        val rightNearImageWidth = arr.getDimension(R.styleable.CustomMenu_cm_right_near_image_width, dp2px(20F)).toInt()
        val rightNearImageHeight = arr.getDimension(R.styleable.CustomMenu_cm_right_near_image_height, dp2px(20F)).toInt()

        val rightDrawableVisible = arr.getBoolean(R.styleable.CustomMenu_cm_right_image_visible, true)
        val rightImageDrawable = arr.getDrawable(R.styleable.CustomMenu_cm_right_image_drawable)
        val rightImageWidth = arr.getDimension(R.styleable.CustomMenu_cm_right_image_width, dp2px(22F)).toInt()
        val rightImageHeight = arr.getDimension(R.styleable.CustomMenu_cm_right_image_height, dp2px(22F)).toInt()

        val bottomDividerVisible = arr.getBoolean(R.styleable.CustomMenu_cm_bottom_divider_visible, false)
        val bottomDividerColor = arr.getColor(R.styleable.CustomMenu_cm_bottom_divider_color,
                ContextCompat.getColor(context, R.color.color_EEEEEE))
        val bottomDividerLeftMargin = arr.getDimension(R.styleable.CustomMenu_cm_bottom_divider_left_margin, dp2px(20F)).toInt()

        iv_left_icon_menu.visibility = if (leftImageVisible) View.VISIBLE else View.GONE
        if (null != leftImageDrawable) {
            iv_left_icon_menu.setImageDrawable(leftImageDrawable)
        }
        val leftImageLp: RelativeLayout.LayoutParams = iv_left_icon_menu.layoutParams as LayoutParams
        leftImageLp.width = leftImageWidth
        leftImageLp.height = leftImageHeight
        iv_left_icon_menu.layoutParams = leftImageLp


        if (!TextUtils.isEmpty(leftTextInfo)) {
            tv_left_name_menu.text = leftTextInfo
            tv_left_name_menu.setTextColor(leftTextColor)
            tv_left_name_menu.setTextSize(TypedValue.COMPLEX_UNIT_DIP, leftTextSize)

            val leftTextLp: RelativeLayout.LayoutParams = tv_left_name_menu.layoutParams as LayoutParams
            leftTextLp.marginStart = leftTextLeftMargin
            tv_left_name_menu.layoutParams = leftTextLp
        }


        tv_right_name_menu.visibility = if (rightTextVisible) View.VISIBLE else View.GONE
        if (!TextUtils.isEmpty(rightTextInfo)) {
            tv_right_name_menu.text = rightTextInfo
            tv_right_name_menu.setTextColor(rightTextColor)
            tv_right_name_menu.setTextSize(TypedValue.COMPLEX_UNIT_DIP, rightTextSize)

            val rightTextLp: RelativeLayout.LayoutParams = tv_right_name_menu.layoutParams as LayoutParams
            if (rightDrawableVisible) {
                rightTextLp.marginEnd = rightTextRightMargin + dp2px(20F).toInt()
            } else {
                rightTextLp.marginEnd = rightTextRightMargin
            }
            tv_right_name_menu.layoutParams = rightTextLp
        }


        iv_near_right_icon_menu.visibility = if (rightNearImageVisible) View.VISIBLE else View.GONE
        if (null != rightNearImageDrawable) {
            iv_near_right_icon_menu.setImageDrawable(rightNearImageDrawable)
        }

        val rightNearImageLp: RelativeLayout.LayoutParams = iv_near_right_icon_menu.layoutParams as LayoutParams
        rightNearImageLp.width = rightNearImageWidth
        rightNearImageLp.height = rightNearImageHeight
        if (rightDrawableVisible) {
            rightNearImageLp.marginEnd = rightTextRightMargin + dp2px(20F).toInt()
        } else {
            rightNearImageLp.marginEnd = rightTextRightMargin
        }
        iv_near_right_icon_menu.layoutParams = rightNearImageLp


        iv_right_arrow_menu.visibility = if (rightDrawableVisible) View.VISIBLE else View.GONE
        if (null != rightImageDrawable) {
            iv_right_arrow_menu.setImageDrawable(rightImageDrawable)
        }

        val rightImageLp: RelativeLayout.LayoutParams = iv_right_arrow_menu.layoutParams as LayoutParams
        rightImageLp.width = rightImageWidth
        rightImageLp.height = rightImag
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值