效果
dimens.xml
<dimen name="title_bar_height">40dp</dimen>
layout_toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/cl_toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/title_bar_height"
android:paddingHorizontal="18dp"
android:background="@color/common_blue"
>
<ImageView
android:id="@+id/iv_back"
android:layout_width="22dp"
android:layout_height="22dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:src="@mipmap/ic_back_white"
android:background="?attr/actionBarItemBackground"
/>
<TextView
android:id="@+id/tv_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:maxLines="1"
android:ellipsize="end"
tools:text="Title"
android:textSize="18sp"
android:textColor="@color/white"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/iv_back"
app:layout_constraintEnd_toStartOf="@id/iv_right"
android:gravity="center"
android:layout_marginHorizontal="20dp" />
<ImageView
android:id="@+id/iv_right"
android:layout_width="22dp"
android:layout_height="22dp"
android:background="?attr/actionBarItemBackground"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
TitleBar.kt
**package com.lzk.libcommon.widget
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import com.lzk.libcommon.R
import com.lzk.libcommon.databinding.LayoutToolbarBinding
class TitleBar(context: Context,attributeSet: AttributeSet) : FrameLayout(context,attributeSet) {
private val mBinding = DataBindingUtil.inflate<LayoutToolbarBinding>(LayoutInflater.from(context),R.layout.layout_toolbar,this,true)
private var mBackIcon: Int? = null
private var mTitle: String? = null
private var mTitleColor: Int? = null
private var mBackgroundColor: Int? = null
private var mActionIcon: Int? = null
private var mOnActionListener: OnActionListener? = null
init {
val typeArray = context.obtainStyledAttributes(attributeSet,R.styleable.TitleBar)
mBackIcon = typeArray.getResourceId(R.styleable.TitleBar_bar_back_icon,R.mipmap.ic_back_white)
mTitle = typeArray.getString(R.styleable.TitleBar_bar_title)
mTitleColor = typeArray.getColor(R.styleable.TitleBar_bar_title_color,ContextCompat.getColor(context,R.color.white))
mBackgroundColor = typeArray.getColor(R.styleable.TitleBar_bar_background_color,ContextCompat.getColor(context,R.color.common_blue))
mActionIcon = typeArray.getResourceId(R.styleable.TitleBar_bar_action_icon,-1)
typeArray.recycle()
initView()
initEvent()
}
private fun initView(){
mBackIcon?.let {
mBinding.ivBack.apply {
setImageResource(it)
setOnClickListener {
mOnActionListener?.onBack()
}
}
}
mTitle?.let {
mBinding.tvTitle.text = it
}
mTitleColor?.let {
mBinding.tvTitle.setTextColor(it)
}
mBackgroundColor?.let {
mBinding.clToolbar.setBackgroundColor(it)
}
mActionIcon?.let {
if (mActionIcon != -1){
mBinding.ivRight.apply {
setImageResource(it)
setOnClickListener {
mOnActionListener?.onActionClick()
}
}
}
}
}
fun setTitle(@StringRes title: Int){
mBinding.tvTitle.text = resources.getString(title)
}
fun setTitle(title: String){
mBinding.tvTitle.text = title
}
private fun initEvent(){
mBinding.ivBack.setOnClickListener {
mOnActionListener?.onBack()
}
}
interface OnActionListener{
fun onBack()
fun onActionClick(){}
}
fun setOnActionListener(listener: OnActionListener){
mOnActionListener = listener
}
}**
Usage
<com.lzk.libcommon.widget.TitleBar
android:id="@+id/title_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:bar_background_color="@android:color/transparent"
app:bar_back_icon="@mipmap/ic_back_white"
app:bar_title="@string/loan_confirmation"
/>
mBinding.titleBar.setOnActionListener(object : TitleBar.OnActionListener{
override fun onBack() {
//todo
}
override fun onActionClick() {
//todo
}
})
|