Android 设置顶部的内容

想说的话

这里遇到一个问题就是设置抽取出来一个类似与以下的标题内容,自己忽然发现之前的时候没有完全理解,所以自己记录一下,之后再次深入理解。
在这里插入图片描述

实现

首先我们要知道顶部左侧图标 中间文字,右侧图标的特别常见,所以我们先将这个抽取出来。

实现一个自定义控件。


import android.annotation.SuppressLint
import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import androidx.constraintlayout.widget.ConstraintLayout
import com.chaunzhi.chuanzhi.R

class TitleView : ConstraintLayout {
    private var tbTitle: Toolbar? = null
    private var tvContent: TextView? = null
    private var tvRight: TextView? = null
    private var viewLine: View? = null
    private var contentName: String? = null
    private var rightName: String? = null
    private var contentSize: Float? = null
    private var rightSize: Float? = null
    private var contentWidth: Float? = null
    private var rightWidth: Float? = null
    private var contentColor: Int? = null
    private var rightColor: Int? = null
    private var lineColor: Int? = null
    private var titleLeftIcon: Int? = null
    private var contexts: Context? = null

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    @SuppressLint("Recycle")
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        val typeArray =
            context.obtainStyledAttributes(attrs, R.styleable.TitleView)

        contentName = typeArray.getString(R.styleable.TitleView_content_name)

        rightName = typeArray.getString(R.styleable.TitleView_right_name)
        contentSize = typeArray.getDimension(R.styleable.TitleView_content_size, 15.0f)
        rightSize = typeArray.getDimension(R.styleable.TitleView_rights_size, 15.0f)
        contentColor = typeArray.getColor(
            R.styleable.TitleView_content_color,
            resources.getColor(android.R.color.black)
        )
        rightColor = typeArray.getColor(
            R.styleable.TitleView_rights_color,
            resources.getColor(android.R.color.black)
        )
        lineColor = typeArray.getColor(
            R.styleable.TitleView_line_color,
            resources.getColor(R.color.dividing_color)
        )
        contentWidth = typeArray.getDimension(R.styleable.TitleView_content_width, 0.0f)
        rightWidth = typeArray.getDimension(R.styleable.TitleView_right_width, 0.0f)
        titleLeftIcon = typeArray.getResourceId(
            R.styleable.TitleView_title_left_icon,
            R.drawable.left_icon
        )
        inints(context)
    }


    private fun inints(context: Context) {
        contexts = context
        val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        val view = inflater.inflate(
            R.layout.title_view, this, true
        )
        // 包裹的Toolbar
        tbTitle = view.findViewById(R.id.tb_title)
        // 内容
        tvContent = view.findViewById(R.id.tv_title_content)
        // 右侧的
        tvRight = view.findViewById(R.id.tv_title_right)
        // 下方的横线
        viewLine = view.findViewById(R.id.view_title_line)
        tvContent?.text = contentName
        tvRight?.text = rightName
        tvContent?.paint?.textSize = contentSize as Float
        tvRight?.paint?.textSize = rightSize as Float
        val contentLayoutparams = tvContent?.layoutParams
        contentLayoutparams?.width = contentWidth?.toInt()
        tvContent?.layoutParams = contentLayoutparams
        val rightLayoutparams = tvRight?.layoutParams
        rightLayoutparams?.width = rightWidth?.toInt()
        tvRight?.layoutParams = rightLayoutparams
        tvContent?.setTextColor(contentColor as Int)
        tvRight?.setTextColor(rightColor as Int)
        viewLine?.setBackgroundColor(lineColor as Int)
        tbTitle?.setNavigationIcon(titleLeftIcon as Int)


    }

    fun setContentsName(contentName: String) {
        tvContent?.text = contentName
    }

    fun setRightsName(rightName: String) {
        tvRight?.text = rightName
    }

    fun setLeftIcon(leftIcon: Drawable) {
        tbTitle?.navigationIcon = leftIcon
    }

    fun setLeftIcon(leftIcon: Int) {
        tbTitle?.setNavigationIcon(leftIcon as Int)
    }

    fun setNavigationOnClickListener(listener: OnClickListener) {
        tbTitle?.setNavigationOnClickListener(listener)
    }

    fun getToolbar(): Toolbar {
        return tbTitle as Toolbar
    }

}

他的自定义属性。

在 res / values/ 创建一个名叫attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TitleView">
        <attr name="content_name" format="string" />
        <attr name="right_name" format="string" />
        <attr name="content_size" format="dimension" />
        <attr name="rights_size" format="dimension" />
        <attr name="content_color" format="color" />
        <attr name="rights_color" format="color" />
        <attr name="line_color" format="color" />
        <attr name="content_width" format="dimension" />
        <attr name="right_width" format="dimension" />
        <attr name="title_left_icon" format="reference" />
    </declare-styleable>
    </resources>

Actviity 设置右侧的icon

这里哦大家可能就要问了,这些知识这只左侧的图标和居中的标题啊,还缺少右侧的图标。
这里我们可以使用Android 的 onCreateOptionsMenu 的 方法。
如果在 Actvitiy 进行使用

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        // inflate 的参数:
        // @MenuRes int menuRes 一个 menu 
        // Menu menu: 使用上方的 menu 就可以了
        menuInflater.inflate(R.menu.home_right_menu, menu)
        return true
    }

需要注意的是

  1. 如果要使用 onCreateOptionsMenu 一定要将自己的theme 主题设置为false ,当然默认为 false。
<item name="windowNoTitle">false</item>
  1. 在 oncreate 的方法中添加 这句话,否则的话左侧会出现设置的 label。(如果在Activity 中 没有设置,那么会使用Application 中设置的)
 supportActionBar?.setDisplayShowTitleEnabled(false)

fragment 中 设置

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        inflater?.inflate(R.menu.home_right_menu, menu)
    }

在 fragment 中需要注意的也是几点:

  1. 在 fragment 的onCreate 方法中设置
  setHasOptionsMenu(true)
  1. 在 Actviity 和 Fragment 中都设置
    Activity 中设置
   supportActionBar?.setDisplayShowTitleEnabled(false)

Fragment 中设置

  (activity as AppCompatActivity).supportActionBar?.setDisplayShowTitleEnabled(false)
  1. 不需要规定Activity 是不是 windowNoTitle 。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值