文章目录
公共item布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/color_transparent">
<RelativeLayout
android:id="@+id/bg_rll"
android:layout_width="match_parent"
android:layout_height="@dimen/title_height"
android:gravity="center_vertical"
android:background="@color/transparent">
<ImageButton
android:id="@+id/img_left"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/dp_10"
android:background="@color/transparent"
android:src="@drawable/ic_back_white"
android:scaleType="centerCrop"
android:visibility="visible" />
<ImageButton
android:id="@+id/img_right"
android:layout_width="@dimen/title_height"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@color/color_transparent"
android:visibility="gone" />
<TextView
android:id="@+id/tv_left"
style="@style/CustomTitleTextStyle"
android:layout_alignParentLeft="true"
android:text=""
android:textColor="@color/title_right_font_color"
android:textSize="@dimen/title_right_font_size"
android:visibility="gone" />
<TextView
android:id="@+id/tv_right"
style="@style/CustomTitleTextStyle"
android:layout_alignParentRight="true"
android:text="预约"
android:textColor="@color/white"
android:textSize="@dimen/title_right_font_size" />
<TextView
android:id="@+id/tv_title_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="80dp"
android:layout_marginRight="80dp"
android:ellipsize="end"
android:gravity="center"
android:singleLine="true"
android:textColor="@color/white"
android:textSize="@dimen/title_content_font_size"
tools:text="舒适控制" />
</RelativeLayout>
<View
android:id="@+id/title_line"
android:layout_width="match_parent"
android:layout_height="@dimen/title_line_height"
android:background="@color/title_line_color"
android:visibility="gone"/>
</LinearLayout>
resources
<declare-styleable name="CustomTopTitleBar">
<attr name="ctb_title" format="string" />
<attr name="ctb_text_left" format="string" />
<attr name="ctb_text_right" format="string" />
<attr name="ctb_show_text_left" format="boolean" />
<attr name="ctb_show_text_right" format="boolean" />
<attr name="ctb_img_left" format="reference" />
<attr name="ctb_img_right" format="reference" />
<attr name="ctb_show_img_left" format="boolean" />
<attr name="ctb_show_img_right" format="boolean" />
<attr name="ctb_back" format="color|reference" />
<attr name="ctb_show_line" format="boolean" />
</declare-styleable>
自定义公共标题栏
import android.content.Context
import android.content.res.TypedArray
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageButton
import android.widget.RelativeLayout
import android.widget.TextView
import com.beans.base.R
import kotlinx.android.synthetic.main.custom_top_title_bar.view.*
class DefaultTopTitleBarWhite : RelativeLayout{
private lateinit var bgRll : RelativeLayout
private lateinit var imgLeft : ImageButton
private lateinit var imgRight : ImageButton
private lateinit var tvLeft : TextView
private lateinit var tvRight : TextView
private lateinit var tvCenter : TextView
private lateinit var titleLine : View
constructor(context : Context?, attrs : AttributeSet?) : super(context, attrs){
init(context)
attrs?.let { retrieveAttributes(attrs) }
}
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)
}
private fun retrieveAttributes(attrs: AttributeSet?){
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.VISIBLE else View.GONE
val strTitle = typedArray.getString(R.styleable.CustomTopTitleBar_ctb_title)
tvCenter.text = strTitle ?: resources.getString(R.string.main_app_name)
val isShowLeftImg = typedArray.getBoolean(R.styleable.CustomTopTitleBar_ctb_show_img_left,true)
imgLeft.visibility = if(isShowLeftImg) View.VISIBLE else View.GONE
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
}
val isShowRightImg = typedArray.getBoolean(R.styleable.CustomTopTitleBar_ctb_show_img_right,false)
imgRight.visibility = if (isShowRightImg) View.VISIBLE else View.GONE
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 = resources.getString(R.string.def_back_right_con)
}else{
tvRight.text = strRight
}
typedArray.recycle()
}
fun setLeftClickListener(onClickListener: View.OnClickListener):DefaultTopTitleBarWhite{
imgLeft.setOnClickListener(onClickListener)
tvLeft.setOnClickListener(onClickListener)
return this
}
fun setLeftImg(leftImg : Drawable):DefaultTopTitleBarWhite{
imgLeft.visibility = View.VISIBLE
imgLeft.setImageDrawable(leftImg)
return this
}
fun setLeftText(leftTitle : String):DefaultTopTitleBarWhite{
tvLeft.text = leftTitle
return this
}
fun setLeftTextColor(color: Int):DefaultTopTitleBarWhite{
tvLeft.setTextColor(color)
return this
}
fun setLeftImgVisible(visible : Boolean):DefaultTopTitleBarWhite{
imgLeft.visibility = if(visible) View.VISIBLE else View.GONE
return this
}
fun setBottomLineVisible(visible : Boolean):DefaultTopTitleBarWhite{
title_line.visibility = if(visible) View.VISIBLE else View.GONE
return this
}
fun setLeftTextVisible(visible: Boolean):DefaultTopTitleBarWhite{
tvLeft.visibility = if(visible) View.VISIBLE else View.GONE
return this
}
fun setRightClickListener(onClickListener: View.OnClickListener):DefaultTopTitleBarWhite{
imgRight.setOnClickListener(onClickListener)
tvRight.setOnClickListener(onClickListener)
return this
}
fun setRightImg(rightImg : Drawable):DefaultTopTitleBarWhite{
imgRight.visibility = View.VISIBLE
imgRight.setImageDrawable(rightImg)
return this
}
fun setRightText(rightTitle : String):DefaultTopTitleBarWhite{
tvRight.text = rightTitle
return this
}
fun setRightTextColor(color: Int):DefaultTopTitleBarWhite{
tvRight.setTextColor(color)
return this
}
fun setRightImgVisible(visible: Boolean):DefaultTopTitleBarWhite{
imgRight.visibility = if (visible) View.VISIBLE else View.GONE
return this
}
fun setRightTextVisible(visible: Boolean):DefaultTopTitleBarWhite{
tvRight.visibility = if (visible) View.VISIBLE else View.GONE
return this
}
fun setCenterTitleText(title : String):DefaultTopTitleBarWhite{
tvCenter.text = title
return this
}
fun setCenterTitleTextColor(color: Int):DefaultTopTitleBarWhite{
tvCenter.setTextColor(color)
return this
}
fun getCenterTitleText() : String {
var title = ""
if(tvCenter != null){
title = tvCenter.text.toString().trim()
}
return title
}
fun setTitleBackColor(color: Int):DefaultTopTitleBarWhite{
bgRll.setBackgroundColor(color)
return this
}
}
使用
<com.beans.base.ui.titlebar.DefaultTopTitleBarWhite
android:id="@+id/server_title_white"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"></com.beans.base.ui.titlebar.DefaultTopTitleBarWhite>
mBinding.serverTitleWhite
.setCenterTitleText(resources.getString(R.string.service_owner_manule))
.setRightTextVisible(false)
.setCenterTitleTextColor(resources.getColor(R.color.color_333333))
.setLeftImg(resources.getDrawable(R.mipmap.ve_back_black_icon))
.setTitleBackColor(resources.getColor(R.color.transparent))
.setBottomLineVisible(false)
.setLeftClickListener(View.OnClickListener { finish() })