第一节:自定义TitleView
import android.app.Activity import android.content.Context import android.graphics.Color import android.util.AttributeSet import android.view.LayoutInflater import android.view.View import android.widget.ImageView import android.widget.RelativeLayout import android.widget.TextView import androidx.core.content.ContextCompat import com.tian.yao.R import androidx.databinding.BindingAdapter class TitleView(context: Context, attrs: AttributeSet?) : RelativeLayout(context, attrs) { var textView: TextView private var tvRight: TextView var ivArrow: ImageView init { LayoutInflater.from(context).inflate(R.layout.layout_title, this, true) val mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleView) val title = mTypedArray.getString(R.styleable.TitleView_titleText) val textRight = mTypedArray.getString(R.styleable.TitleView_rightText) val arrowColorTint = mTypedArray.getColor(R.styleable.TitleView_arrowColor, Color.parseColor("#595959")) val textColor = mTypedArray.getColor(R.styleable.TitleView_titleColor, ContextCompat.getColor(context, R.color.color_171921)) val textRightColor = mTypedArray.getColor(R.styleable.TitleView_rightColor, ContextCompat.getColor(context, R.color.color_FFFA2614)) textView = findViewById(R.id.tv_title) textView.setTextColor(textColor) tvRight = findViewById(R.id.tv_right) tvRight.setTextColor(textRightColor) ivArrow = findViewById(R.id.iv_back) ivArrow.setColorFilter(arrowColorTint) ivArrow.setOnClickListener { if (context is Activity) { context.finish() } } textView.text = title tvRight.text = textRight mTypedArray.recycle() } fun setText(title: String) { textView.text = title } fun getText(): String { return textView.text.toString() } fun setRightText(right: String) { tvRight.text = right } fun setRightTextColor(color: Int) { tvRight.setTextColor(color) } fun setIvArrowListener(l: (View) -> Unit) { ivArrow.setOnClickListener(l) } fun setRightListener(l: (View) -> Unit) { tvRight.setOnClickListener(l) } fun setRightEnable(enabled: Boolean) { tvRight.isEnabled = enabled } companion object { @JvmStatic @BindingAdapter("onRightClick") fun onRightClick(titleView: TitleView, onClickListener: OnClickListener) { titleView.tvRight.setOnClickListener(onClickListener) } } }
布局文件 layout_title:
<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="?actionBarSize" android:orientation="vertical"> <ImageView android:id="@+id/iv_back" android:layout_width="50dp" android:layout_height="?actionBarSize" android:layout_gravity="center" android:background="?attr/selectableItemBackground" android:scaleType="centerInside" app:srcCompat="@drawable/svg_back" /> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toEndOf="@id/iv_back" android:gravity="center" android:textColor="#171921" android:textSize="19sp" /> <TextView android:id="@+id/tv_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_centerVertical="true" android:paddingEnd="20dp" android:paddingStart="10dp" android:gravity="center" android:textColor="#3C3C3C" android:textSize="16sp" /> </merge>
布局引用:
<?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> <variable name="listener" type="com.tian.yao.sectionone.SearchAction" /> </data> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <com.tian.yao.sectionone.TitleView android:id="@+id/titleView" android:layout_width="match_parent" android:layout_height="wrap_content" android:adjustViewBounds="true" android:scaleType="centerInside" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:onRightClick="@{v->listener.inquiry()}" app:rightText="查询" app:tint="#999999" app:titleText="" /> </androidx.constraintlayout.widget.ConstraintLayout> </layout>
定义接口:
interface SearchAction { fun inquiry() }
主布局调用:
import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Toast import com.tian.yao.sectionone.TitleView class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) findViewById<TitleView>(R.id.titleView).setRightListener { Toast.makeText(this,"查询",Toast.LENGTH_SHORT).show() } } }
启用dataBinding,在app/build.gradle的android目录下:
buildFeatures { dataBinding true }
插件引用配合BindingAdapter
plugins { id 'com.android.application' id 'kotlin-android' id 'kotlin-kapt' }
定义颜色值:
<color name="color_171921">#171921</color> <color name="color_FFFA2614">#FFFA2614</color>
代码链接:
链接: https://pan.baidu.com/s/1aqpcuhVgIkCr2rfnDV_6ng 提取码: rrea 复制这段内容后打开百度网盘手机App,操作更方便哦