重新自定义titlebar
This commit is contained in:
parent
0f2c26f693
commit
667a922d97
|
|
@ -4,7 +4,11 @@ import android.app.Activity
|
|||
import android.content.Intent
|
||||
import android.view.LayoutInflater
|
||||
import androidx.activity.viewModels
|
||||
import androidx.core.view.ViewCompat
|
||||
import androidx.core.view.WindowInsetsCompat
|
||||
import androidx.core.view.updatePadding
|
||||
import com.ama.core.architecture.appBase.AppViewsActivity
|
||||
import com.gamedog.vididin.R
|
||||
import com.gamedog.vididin.main.interfaces.OnTabStyleListener
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlin.getValue
|
||||
|
|
@ -20,7 +24,21 @@ class WinRecordsActivity : AppViewsActivity<ViewBinding, UiState, ViewModel>(),
|
|||
override fun inflateViewBinding(inflater: LayoutInflater) = ViewBinding.inflate(inflater)
|
||||
|
||||
override fun ViewBinding.initViews() {
|
||||
//TODO("Not yet implemented")
|
||||
|
||||
with(binding) {
|
||||
titlebar.setBackIconColor(R.color.black)
|
||||
titlebar.setTitleText(R.string.lottery_record, R.color.black)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
override fun ViewBinding.initWindowInsets() {
|
||||
ViewCompat.setOnApplyWindowInsetsListener(contentRoot) { v, insets ->
|
||||
val systemBars =
|
||||
insets.getInsets(WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout())
|
||||
v.updatePadding(top = systemBars.top)
|
||||
insets
|
||||
}
|
||||
}
|
||||
|
||||
override fun ViewBinding.initListeners() {
|
||||
|
|
|
|||
|
|
@ -33,6 +33,9 @@ class ZeroBuyActivity : AppViewsActivity<ViewBinding, UiState, ViewModel>(), OnT
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,199 +2,209 @@
|
|||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginHorizontal="15dp"
|
||||
android:orientation="vertical">
|
||||
android:orientation="vertical"
|
||||
android:id="@+id/content_root" >
|
||||
|
||||
|
||||
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
android:id="@+id/tv_date_win"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="12sp"
|
||||
android:textColor="@color/gray6"
|
||||
android:text="2025/10/31"
|
||||
/>
|
||||
|
||||
|
||||
<RelativeLayout
|
||||
<com.ama.core.architecture.widget.CustomTitleBar
|
||||
android:id="@+id/titlebar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:background="@drawable/bg_records_win"
|
||||
android:layout_marginTop="10dp"
|
||||
android:gravity="center_vertical"
|
||||
android:paddingVertical="5dp"
|
||||
>
|
||||
|
||||
<androidx.appcompat.widget.AppCompatImageView
|
||||
android:id="@+id/iv_left_win"
|
||||
android:layout_width="45dp"
|
||||
android:layout_height="45dp"
|
||||
android:src="@mipmap/record_win"
|
||||
android:layout_centerVertical="true"
|
||||
android:layout_marginLeft="15dp"
|
||||
/>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:layout_marginHorizontal="10dp"
|
||||
android:layout_toRightOf="@+id/iv_left_win"
|
||||
android:layout_toLeftOf="@+id/ll_right_win"
|
||||
android:layout_centerVertical="true"
|
||||
>
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
android:id="@+id/tv_title_win"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="15sp"
|
||||
android:textColor="@color/white"
|
||||
android:text="@string/record_win_item_title"
|
||||
/>
|
||||
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
android:id="@+id/tv_desc_win"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="5dp"
|
||||
android:textSize="14sp"
|
||||
android:textColor="@color/white_alpha_99"
|
||||
android:text="@string/record_win_item_desc"
|
||||
/>
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/ll_right_win"
|
||||
android:layout_width="75dp"
|
||||
android:layout_height="75dp"
|
||||
android:orientation="vertical"
|
||||
android:layout_alignParentRight="true"
|
||||
android:layout_centerVertical="true"
|
||||
android:layout_marginRight="5dp"
|
||||
android:background="@mipmap/bg_record_win_rgiht"
|
||||
android:paddingHorizontal="6dp"
|
||||
android:gravity="center_horizontal"
|
||||
>
|
||||
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
android:id="@+id/tv_win_right_top"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="10dp"
|
||||
android:textStyle="bold"
|
||||
android:textSize="15sp"
|
||||
android:textColor="@color/yellow_00"
|
||||
android:text="Ganhar"
|
||||
/>
|
||||
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
android:id="@+id/tv_win_right_bottom"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="11dp"
|
||||
android:textSize="10sp"
|
||||
android:textColor="@color/green_54"
|
||||
android:text="Ver detalhes"
|
||||
/>
|
||||
</LinearLayout>
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
|
||||
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
android:id="@+id/tv_date_lost"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="15dp"
|
||||
android:textSize="12sp"
|
||||
android:textColor="@color/gray6"
|
||||
android:text="2025/10/31"
|
||||
/>
|
||||
|
||||
|
||||
|
||||
<RelativeLayout
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:background="@drawable/bg_records_lost"
|
||||
android:layout_marginTop="10dp"
|
||||
android:gravity="center_vertical"
|
||||
android:paddingVertical="5dp"
|
||||
>
|
||||
android:layout_marginHorizontal="15dp"
|
||||
android:orientation="vertical">
|
||||
|
||||
<androidx.appcompat.widget.AppCompatImageView
|
||||
android:id="@+id/iv_left_lost"
|
||||
android:layout_width="45dp"
|
||||
android:layout_height="45dp"
|
||||
android:src="@mipmap/record_lost"
|
||||
android:layout_centerVertical="true"
|
||||
android:layout_marginLeft="15dp"
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
android:id="@+id/tv_date_win"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="15dp"
|
||||
android:textSize="12sp"
|
||||
android:textColor="@color/gray6"
|
||||
android:text="2025/10/31"
|
||||
/>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="wrap_content"
|
||||
|
||||
<RelativeLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:layout_marginHorizontal="10dp"
|
||||
android:layout_toRightOf="@+id/iv_left_lost"
|
||||
android:layout_toLeftOf="@+id/ll_right_lost"
|
||||
android:layout_centerVertical="true"
|
||||
android:orientation="horizontal"
|
||||
android:background="@drawable/bg_records_win"
|
||||
android:layout_marginTop="5dp"
|
||||
android:gravity="center_vertical"
|
||||
android:paddingVertical="5dp"
|
||||
>
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
android:id="@+id/tv_title_lost"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="15sp"
|
||||
android:textColor="@color/white"
|
||||
android:text="@string/record_lost_item_title"
|
||||
|
||||
<androidx.appcompat.widget.AppCompatImageView
|
||||
android:id="@+id/iv_left_win"
|
||||
android:layout_width="45dp"
|
||||
android:layout_height="45dp"
|
||||
android:src="@mipmap/record_win"
|
||||
android:layout_centerVertical="true"
|
||||
android:layout_marginLeft="15dp"
|
||||
/>
|
||||
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
android:id="@+id/tv_desc_lost"
|
||||
<LinearLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="5dp"
|
||||
android:textSize="14sp"
|
||||
android:textColor="@color/white_alpha_99"
|
||||
android:text="@string/record_lost_item_desc"
|
||||
/>
|
||||
</LinearLayout>
|
||||
android:orientation="vertical"
|
||||
android:layout_marginHorizontal="10dp"
|
||||
android:layout_toRightOf="@+id/iv_left_win"
|
||||
android:layout_toLeftOf="@+id/ll_right_win"
|
||||
android:layout_centerVertical="true"
|
||||
>
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
android:id="@+id/tv_title_win"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="15sp"
|
||||
android:textColor="@color/white"
|
||||
android:text="@string/record_win_item_title"
|
||||
/>
|
||||
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
android:id="@+id/tv_desc_win"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="5dp"
|
||||
android:textSize="14sp"
|
||||
android:textColor="@color/white_alpha_99"
|
||||
android:text="@string/record_win_item_desc"
|
||||
/>
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
<FrameLayout
|
||||
android:id="@+id/ll_right_lost"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:layout_alignParentEnd="true"
|
||||
android:layout_centerVertical="true"
|
||||
android:layout_marginEnd="5dp"
|
||||
android:background="@mipmap/bg_record_lost_rgiht"
|
||||
android:paddingHorizontal="6dp"
|
||||
android:gravity="center"
|
||||
>
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
android:id="@+id/tv_lost_right_top"
|
||||
<LinearLayout
|
||||
android:id="@+id/ll_right_win"
|
||||
android:layout_width="75dp"
|
||||
android:layout_height="75dp"
|
||||
android:textStyle="bold"
|
||||
android:gravity="center"
|
||||
android:layout_gravity="center"
|
||||
android:textSize="15sp"
|
||||
android:textColor="@color/gray9"
|
||||
android:text="@string/not_win"
|
||||
android:orientation="vertical"
|
||||
android:layout_alignParentRight="true"
|
||||
android:layout_centerVertical="true"
|
||||
android:layout_marginRight="5dp"
|
||||
android:background="@mipmap/bg_record_win_rgiht"
|
||||
android:paddingHorizontal="6dp"
|
||||
android:gravity="center_horizontal"
|
||||
>
|
||||
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
android:id="@+id/tv_win_right_top"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="10dp"
|
||||
android:textStyle="bold"
|
||||
android:textSize="15sp"
|
||||
android:textColor="@color/yellow_00"
|
||||
android:text="Ganhar"
|
||||
/>
|
||||
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
android:id="@+id/tv_win_right_bottom"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="11dp"
|
||||
android:textSize="10sp"
|
||||
android:textColor="@color/green_54"
|
||||
android:text="Ver detalhes"
|
||||
/>
|
||||
</LinearLayout>
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
|
||||
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
android:id="@+id/tv_date_lost"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="15dp"
|
||||
android:textSize="12sp"
|
||||
android:textColor="@color/gray6"
|
||||
android:text="2025/10/31"
|
||||
/>
|
||||
|
||||
|
||||
|
||||
<RelativeLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:background="@drawable/bg_records_lost"
|
||||
android:layout_marginTop="5dp"
|
||||
android:gravity="center_vertical"
|
||||
android:paddingVertical="5dp"
|
||||
>
|
||||
|
||||
<androidx.appcompat.widget.AppCompatImageView
|
||||
android:id="@+id/iv_left_lost"
|
||||
android:layout_width="45dp"
|
||||
android:layout_height="45dp"
|
||||
android:src="@mipmap/record_lost"
|
||||
android:layout_centerVertical="true"
|
||||
android:layout_marginLeft="15dp"
|
||||
/>
|
||||
</FrameLayout>
|
||||
|
||||
</RelativeLayout>
|
||||
<LinearLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:layout_marginHorizontal="10dp"
|
||||
android:layout_toRightOf="@+id/iv_left_lost"
|
||||
android:layout_toLeftOf="@+id/ll_right_lost"
|
||||
android:layout_centerVertical="true"
|
||||
>
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
android:id="@+id/tv_title_lost"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="15sp"
|
||||
android:textColor="@color/white"
|
||||
android:text="@string/record_lost_item_title"
|
||||
/>
|
||||
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
android:id="@+id/tv_desc_lost"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="5dp"
|
||||
android:textSize="14sp"
|
||||
android:textColor="@color/white_alpha_99"
|
||||
android:text="@string/record_lost_item_desc"
|
||||
/>
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
<FrameLayout
|
||||
android:id="@+id/ll_right_lost"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:layout_alignParentEnd="true"
|
||||
android:layout_centerVertical="true"
|
||||
android:layout_marginEnd="5dp"
|
||||
android:background="@mipmap/bg_record_lost_rgiht"
|
||||
android:paddingHorizontal="6dp"
|
||||
android:gravity="center"
|
||||
>
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
android:id="@+id/tv_lost_right_top"
|
||||
android:layout_width="75dp"
|
||||
android:layout_height="75dp"
|
||||
android:textStyle="bold"
|
||||
android:gravity="center"
|
||||
android:layout_gravity="center"
|
||||
android:textSize="15sp"
|
||||
android:textColor="@color/gray9"
|
||||
android:text="@string/not_win"
|
||||
/>
|
||||
</FrameLayout>
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
|
|
|||
|
|
@ -38,6 +38,7 @@
|
|||
<string name="record_lost_item_desc">Sorteio 333 - Grupo de 10</string>
|
||||
<string name="record_win_item_desc">Sorteio 333 - Grupo de 100</string>
|
||||
<string name="zero_menu_records">Meus Prêmios</string>
|
||||
<string name="lottery_record">Histórico de Prêmios</string>
|
||||
|
||||
|
||||
</resources>
|
||||
|
|
@ -1,255 +1,113 @@
|
|||
package com.ama.core.architecture.widget
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.res.ColorStateList
|
||||
import android.util.AttributeSet
|
||||
import android.view.Gravity
|
||||
import android.widget.ImageView
|
||||
import android.view.LayoutInflater
|
||||
import android.widget.FrameLayout
|
||||
import android.widget.LinearLayout
|
||||
import android.widget.TextView
|
||||
import androidx.annotation.DrawableRes
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.view.setPadding
|
||||
import androidx.appcompat.widget.AppCompatImageView
|
||||
import androidx.appcompat.widget.AppCompatTextView
|
||||
import androidx.core.view.isVisible
|
||||
import com.ama.core.architecture.R
|
||||
import com.ama.core.architecture.databinding.CustomTitleBarBinding
|
||||
import com.ama.core.architecture.util.ResUtil
|
||||
import com.ama.core.architecture.util.setOnClickBatch
|
||||
import com.ama.core.common.util.dp
|
||||
import com.ama.core.common.util.sp
|
||||
|
||||
|
||||
class CustomTitleBar @JvmOverloads constructor(
|
||||
context: Context,
|
||||
attrs: AttributeSet? = null,
|
||||
defStyleAttr: Int = 0
|
||||
) : LinearLayout(context, attrs, defStyleAttr) {
|
||||
) : FrameLayout(context, attrs, defStyleAttr) {
|
||||
|
||||
private lateinit var ivLeft: ImageView
|
||||
private lateinit var tvTitle: TextView
|
||||
private lateinit var llRight: LinearLayout
|
||||
private var mBinding: CustomTitleBarBinding
|
||||
|
||||
// 点击回调
|
||||
private var onLeftClick: (() -> Unit)? = null
|
||||
private var onRightButtonClick: ((index: Int) -> Unit)? = null
|
||||
|
||||
// 右侧按钮列表
|
||||
private val rightButtons = mutableListOf<ImageView>()
|
||||
|
||||
init {
|
||||
initView()
|
||||
initAttributes(attrs, defStyleAttr)
|
||||
}
|
||||
mBinding = CustomTitleBarBinding.inflate(LayoutInflater.from(context), this, true)
|
||||
|
||||
private fun initView() {
|
||||
// 设置水平方向布局
|
||||
orientation = HORIZONTAL
|
||||
gravity = Gravity.CENTER_VERTICAL
|
||||
mBinding.run {
|
||||
setOnClickBatch(ivBack, tvCenterTitle) {
|
||||
when(this) {
|
||||
ivBack -> {
|
||||
(context as? Activity)?.let { activity ->
|
||||
activity.finish()
|
||||
}
|
||||
}
|
||||
|
||||
// 设置默认内边距
|
||||
setPadding(16.dp)
|
||||
tvCenterTitle -> {
|
||||
|
||||
// 创建左侧ImageView
|
||||
ivLeft = ImageView(context).apply {
|
||||
layoutParams = LayoutParams(
|
||||
24.dp,
|
||||
24.dp
|
||||
).apply {
|
||||
gravity = Gravity.CENTER_VERTICAL
|
||||
}
|
||||
scaleType = ImageView.ScaleType.CENTER_INSIDE
|
||||
setOnClickListener { onLeftClick?.invoke() }
|
||||
}
|
||||
|
||||
// 创建中间标题TextView
|
||||
tvTitle = TextView(context).apply {
|
||||
layoutParams = LayoutParams(
|
||||
LayoutParams.WRAP_CONTENT,
|
||||
LayoutParams.WRAP_CONTENT
|
||||
).apply {
|
||||
weight = 1f // 占据剩余空间
|
||||
gravity = Gravity.CENTER
|
||||
}
|
||||
gravity = Gravity.CENTER
|
||||
setTextColor(ContextCompat.getColor(context, android.R.color.white))
|
||||
textSize = 18f
|
||||
}
|
||||
|
||||
// 创建右侧LinearLayout
|
||||
llRight = LinearLayout(context).apply {
|
||||
layoutParams = LayoutParams(
|
||||
LayoutParams.WRAP_CONTENT,
|
||||
LayoutParams.WRAP_CONTENT
|
||||
).apply {
|
||||
gravity = Gravity.CENTER_VERTICAL or Gravity.END
|
||||
}
|
||||
orientation = HORIZONTAL
|
||||
gravity = Gravity.CENTER_VERTICAL
|
||||
}
|
||||
|
||||
// 添加子视图
|
||||
addView(ivLeft)
|
||||
addView(tvTitle)
|
||||
addView(llRight)
|
||||
}
|
||||
|
||||
private fun initAttributes(attrs: AttributeSet?, defStyleAttr: Int) {
|
||||
val typedArray = context.obtainStyledAttributes(
|
||||
attrs,
|
||||
R.styleable.CustomTitleBar,
|
||||
defStyleAttr,
|
||||
0
|
||||
)
|
||||
|
||||
try {
|
||||
// 设置标题文本
|
||||
tvTitle.text = typedArray.getString(R.styleable.CustomTitleBar_titleText) ?: ""
|
||||
|
||||
// 设置标题颜色
|
||||
val titleColor = typedArray.getColor(
|
||||
R.styleable.CustomTitleBar_titleTextColor,
|
||||
ContextCompat.getColor(context, android.R.color.white)
|
||||
)
|
||||
tvTitle.setTextColor(titleColor)
|
||||
|
||||
// 设置标题大小
|
||||
val titleSize = typedArray.getDimension(
|
||||
R.styleable.CustomTitleBar_titleTextSize,
|
||||
18f.sp.toFloat()
|
||||
)
|
||||
tvTitle.textSize = titleSize.sp.toFloat()
|
||||
|
||||
// 设置左侧图标
|
||||
val leftIconRes = typedArray.getResourceId(R.styleable.CustomTitleBar_leftIcon, 0)
|
||||
if (leftIconRes != 0) {
|
||||
setLeftIcon(leftIconRes)
|
||||
}
|
||||
|
||||
// 设置左侧图标可见性
|
||||
val leftIconVisible = typedArray.getBoolean(
|
||||
R.styleable.CustomTitleBar_leftIconVisible,
|
||||
true
|
||||
)
|
||||
ivLeft.visibility = if (leftIconVisible) VISIBLE else GONE
|
||||
|
||||
// 设置背景颜色
|
||||
val background = typedArray.getColor(
|
||||
R.styleable.CustomTitleBar_titleBarBackground,
|
||||
ContextCompat.getColor(context, R.color.title_bar_default_bg)
|
||||
)
|
||||
setBackgroundColor(background)
|
||||
|
||||
// 设置右侧按钮间距
|
||||
val buttonSpacing = typedArray.getDimension(
|
||||
R.styleable.CustomTitleBar_rightButtonSpacing,
|
||||
8.dp.toFloat()
|
||||
)
|
||||
llRight.setPadding(0, 0, 0, 0)
|
||||
|
||||
} finally {
|
||||
typedArray.recycle()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置左侧图标
|
||||
*/
|
||||
fun setLeftIcon(@DrawableRes resId: Int) {
|
||||
ivLeft.setImageResource(resId)
|
||||
ivLeft.visibility = VISIBLE
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置左侧图标可见性
|
||||
*/
|
||||
fun setLeftIconVisible(visible: Boolean) {
|
||||
ivLeft.visibility = if (visible) VISIBLE else GONE
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置标题文本
|
||||
*/
|
||||
fun setTitle(text: String) {
|
||||
tvTitle.text = text
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置标题颜色
|
||||
*/
|
||||
fun setTitleColor(color: Int) {
|
||||
tvTitle.setTextColor(color)
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置标题大小
|
||||
*/
|
||||
fun setTitleSize(sp: Float) {
|
||||
tvTitle.textSize = sp
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加右侧按钮
|
||||
*/
|
||||
fun addRightButton(@DrawableRes resId: Int, spacing: Int = 8.dp): Int {
|
||||
val imageView = ImageView(context).apply {
|
||||
layoutParams = LayoutParams(
|
||||
24.dp,
|
||||
24.dp
|
||||
).apply {
|
||||
if (rightButtons.isNotEmpty()) {
|
||||
marginStart = spacing
|
||||
}
|
||||
}
|
||||
}
|
||||
setImageResource(resId)
|
||||
scaleType = ImageView.ScaleType.CENTER_INSIDE
|
||||
setOnClickListener {
|
||||
onRightButtonClick?.invoke(rightButtons.size - 1)
|
||||
}
|
||||
}
|
||||
|
||||
rightButtons.add(imageView)
|
||||
llRight.addView(imageView)
|
||||
|
||||
return rightButtons.size - 1
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除右侧按钮
|
||||
*/
|
||||
fun removeRightButton(index: Int) {
|
||||
if (index in 0 until rightButtons.size) {
|
||||
llRight.removeView(rightButtons[index])
|
||||
rightButtons.removeAt(index)
|
||||
// 更新剩余按钮的点击事件索引
|
||||
updateRightButtonsClickListeners()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空右侧按钮
|
||||
*/
|
||||
fun clearRightButtons() {
|
||||
llRight.removeAllViews()
|
||||
rightButtons.clear()
|
||||
fun setTitleText(textRes: Int, textColor: Int = R.color.white) {
|
||||
mBinding.tvCenterTitle.text = ResUtil.getString(textRes)
|
||||
mBinding.tvCenterTitle.setTextColor(ResUtil.getColor(textColor))
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置左侧点击回调
|
||||
*/
|
||||
fun setOnLeftClickListener(listener: () -> Unit) {
|
||||
onLeftClick = listener
|
||||
fun hideBack() {
|
||||
mBinding.ivBack.isVisible = false
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置右侧按钮点击回调
|
||||
*/
|
||||
fun setOnRightButtonClickListener(listener: (index: Int) -> Unit) {
|
||||
onRightButtonClick = listener
|
||||
fun setBackIconColor(color: Int) {
|
||||
mBinding.ivBack.imageTintList = ColorStateList.valueOf(ResUtil.getColor(color))
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新右侧按钮点击监听器
|
||||
*/
|
||||
private fun updateRightButtonsClickListeners() {
|
||||
rightButtons.forEachIndexed { index, imageView ->
|
||||
imageView.setOnClickListener {
|
||||
onRightButtonClick?.invoke(index)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
fun addLeftIcon(iconRes: Int, onClicked: ()->Unit) {
|
||||
val newImageView = AppCompatImageView(context)
|
||||
newImageView.setImageResource(iconRes)
|
||||
|
||||
var layoutParam = LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)
|
||||
layoutParam.marginStart = 15.dp
|
||||
layoutParam.gravity = Gravity.CENTER_VERTICAL
|
||||
|
||||
mBinding.llLeftRoot.addView(newImageView, layoutParam)
|
||||
}
|
||||
|
||||
}
|
||||
fun addLeftText(textRes: Int, onClicked: ()->Unit) {
|
||||
val newImageView = AppCompatTextView(context)
|
||||
newImageView.text = ResUtil.getString(textRes)
|
||||
|
||||
var layoutParam = LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)
|
||||
layoutParam.marginStart = 15.dp
|
||||
layoutParam.gravity = Gravity.CENTER_VERTICAL
|
||||
|
||||
mBinding.llLeftRoot.addView(newImageView, layoutParam)
|
||||
}
|
||||
|
||||
fun addRightIcon(iconRes: Int, onClicked: ()->Unit) {
|
||||
val newImageView = AppCompatImageView(context)
|
||||
newImageView.setImageResource(iconRes)
|
||||
|
||||
var layoutParam = LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)
|
||||
layoutParam.marginStart = 15.dp
|
||||
layoutParam.gravity = Gravity.CENTER_VERTICAL
|
||||
|
||||
mBinding.llRightRoot.addView(newImageView, layoutParam)
|
||||
}
|
||||
|
||||
fun addRightText(textRes: Int, onClicked: ()->Unit) {
|
||||
val newImageView = AppCompatTextView(context)
|
||||
newImageView.text = ResUtil.getString(textRes)
|
||||
|
||||
var layoutParam = LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)
|
||||
layoutParam.marginStart = 15.dp
|
||||
layoutParam.gravity = Gravity.CENTER_VERTICAL
|
||||
|
||||
mBinding.llRightRoot.addView(newImageView, layoutParam)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,64 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/titlebar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="44dp"
|
||||
tools:ignore="ResourceName"
|
||||
>
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/ll_left_root"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="match_parent"
|
||||
android:gravity="center_vertical"
|
||||
android:orientation="vertical">
|
||||
|
||||
<androidx.appcompat.widget.AppCompatImageView
|
||||
android:id="@+id/iv_back"
|
||||
android:layout_width="wrap_content"
|
||||
android:paddingLeft="16dp"
|
||||
android:layout_height="match_parent"
|
||||
android:src="@mipmap/icon_back"
|
||||
/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<FrameLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_centerInParent="true"
|
||||
android:layout_toEndOf="@id/ll_left_root"
|
||||
android:layout_toStartOf="@+id/ll_right_root"
|
||||
android:gravity="center_vertical"
|
||||
android:orientation="vertical">
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/ll_center_root"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_gravity="center"
|
||||
android:gravity="center_vertical"
|
||||
android:orientation="horizontal">
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
android:id="@+id/tv_center_title"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Title"
|
||||
android:textColor="@color/white"
|
||||
android:textSize="21sp"
|
||||
android:textStyle="bold" />
|
||||
</LinearLayout>
|
||||
</FrameLayout>
|
||||
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/ll_right_root"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="match_parent"
|
||||
android:gravity="center_vertical"
|
||||
android:layout_alignParentEnd="true"
|
||||
android:orientation="vertical">
|
||||
</LinearLayout>
|
||||
|
||||
</RelativeLayout>
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 692 B |
|
|
@ -50,6 +50,7 @@
|
|||
|
||||
<color name="transparent">00000000</color>
|
||||
<color name="title_bar_default_bg">#FF6B9E</color>
|
||||
<color name="white">#FFFFFF</color>
|
||||
|
||||
|
||||
</resources>
|
||||
Loading…
Reference in New Issue