fix:【光环助手】客户端UI组件规范更新同步2024/06/17 https://jira.shanqu.cc/browse/GHZSCY-5738

This commit is contained in:
张晨
2024-11-11 18:04:22 +08:00
parent 0f9f0b7c9a
commit 6c8ea6ffb2
5 changed files with 325 additions and 7 deletions

View File

@ -0,0 +1,253 @@
package com.gh.gamecenter.common.view.titlebar
import android.content.Context
import android.content.res.ColorStateList
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.content.res.AppCompatResources
import androidx.appcompat.view.ContextThemeWrapper
import androidx.constraintlayout.widget.ConstraintLayout
import com.gh.gamecenter.common.R
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.toColor
import splitties.views.dsl.constraintlayout.*
import splitties.views.dsl.core.add
import splitties.views.dsl.core.matchParent
import splitties.views.dsl.core.wrapContent
/**
* 面板标题栏
* 一般用于底部拉起面板
*/
class BottomSheetTitleView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0,
defStyleRes: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr, defStyleRes) {
private lateinit var config: Config
private var leftView: View? = null
private var rightView: View? = null
init {
setBackgroundResource(R.drawable.background_shape_white_radius_12_top_only)
initAttrs(context, attrs)
}
private fun initAttrs(context: Context, attrs: AttributeSet?) {
val typedArray = context.obtainStyledAttributes(attrs, R.styleable.BottomSheetTitleView)
val leftStyle = typedArray.getInt(R.styleable.BottomSheetTitleView_left_style, BUTTON_STYLE_TEXT)
val leftText = typedArray.getString(R.styleable.BottomSheetTitleView_left_text) ?: ""
val leftTextColor = typedArray.getColor(R.styleable.BottomSheetTitleView_left_text_color, -1)
val leftIconResource = typedArray.getResourceId(R.styleable.BottomSheetTitleView_left_icon_resource, -1)
val leftIconColor = typedArray.getColor(R.styleable.BottomSheetTitleView_left_icon_color, -1)
val leftButtonBackgroundResource =
typedArray.getResourceId(R.styleable.BottomSheetTitleView_left_button_background_resource, -1)
val title = typedArray.getString(R.styleable.BottomSheetTitleView_title) ?: ""
val rightStyle = typedArray.getInt(R.styleable.BottomSheetTitleView_right_style, -1)
val rightText = typedArray.getString(R.styleable.BottomSheetTitleView_right_text) ?: ""
val rightTextColor = typedArray.getColor(R.styleable.BottomSheetTitleView_right_text_color, -1)
val rightIconResource = typedArray.getResourceId(R.styleable.BottomSheetTitleView_right_icon_resource, -1)
val rightIconColor = typedArray.getColor(R.styleable.BottomSheetTitleView_right_icon_color, -1)
val rightButtonBackgroundResource =
typedArray.getResourceId(R.styleable.BottomSheetTitleView_right_button_background_resource, -1)
config = Config(
leftStyle = leftStyle,
leftText = leftText,
leftTextColor = leftTextColor,
leftIconResource = leftIconResource,
leftIconColor = leftIconColor,
leftButtonBackgroundResource = leftButtonBackgroundResource,
title = title,
rightStyle = rightStyle,
rightText = rightText,
rightTextColor = rightTextColor,
rightIconResource = rightIconResource,
rightIconColor = rightIconColor,
rightButtonBackgroundResource = rightButtonBackgroundResource
)
typedArray.recycle()
initView()
}
private fun initView() {
leftView = when (config.leftStyle) {
BUTTON_STYLE_TEXT -> createTextView(context, true)
BUTTON_STYLE_ICON -> createIconView(context, true)
BUTTON_STYLE_BUTTON -> createButtonView(context, true)
BUTTON_STYLE_TITLE -> createTitleView(context)
else -> null
}
leftView?.let {
add(it, lParams(it.wrapContent, it.matchParent) {
startOfParent()
topOfParent()
bottomOfParent()
})
}
rightView = when (config.rightStyle) {
BUTTON_STYLE_TEXT -> createTextView(context, false)
BUTTON_STYLE_ICON -> createIconView(context, false)
BUTTON_STYLE_BUTTON -> createButtonView(context, false)
else -> null
}
rightView?.let {
add(it, lParams(it.wrapContent, it.matchParent) {
endOfParent()
topOfParent()
bottomOfParent()
})
}
if (config.leftStyle != BUTTON_STYLE_TITLE) {
createTitleView(context).let {
add(it, lParams(it.wrapContent, it.matchParent) {
endOfParent()
startOfParent()
topOfParent()
bottomOfParent()
})
}
}
}
private fun createTitleView(context: Context): View {
val themedContext = ContextThemeWrapper(context, R.style.TextHeadline)
val textView = TextView(themedContext)
textView.setPadding(16F.dip2px(), 0, 16F.dip2px(), 0)
textView.gravity = Gravity.CENTER
textView.setTextColor(R.color.text_primary.toColor(context))
textView.text = config.title
return textView
}
private fun createButtonView(
context: Context,
isLeft: Boolean
): View {
val backgroundResId = if (isLeft) {
config.leftButtonBackgroundResource
} else {
config.rightButtonBackgroundResource
}
val textColor = if (isLeft) {
config.leftTextColor
} else {
config.rightTextColor
}
val text = if (isLeft) {
config.leftText
} else {
config.rightText
}
val themedContext = ContextThemeWrapper(context, R.style.BtnMiniStyle)
val textView = TextView(themedContext)
if (backgroundResId == -1) {
textView.setBackgroundResource(R.drawable.bg_common_button_fill_blue)
} else {
textView.setBackgroundResource(backgroundResId)
}
textView.height = 24F.dip2px()
if (textColor == -1) {
textView.setTextColor(R.color.text_aw_primary.toColor(context))
} else {
textView.setTextColor(textColor)
}
textView.text = text
val btnContainer = LinearLayout(context)
btnContainer.gravity = Gravity.CENTER
btnContainer.setPadding(16F.dip2px(), 0, 16F.dip2px(), 0)
btnContainer.addView(textView)
return btnContainer
}
private fun createIconView(context: Context, isLeft: Boolean): View {
val iconResId = if (isLeft) config.leftIconResource else config.rightIconResource
val iconColor = if (isLeft) config.leftIconColor else config.rightIconColor
val imageView = ImageView(context)
if (iconResId != -1) {
val drawable = AppCompatResources.getDrawable(context, iconResId)
imageView.setImageDrawable(drawable)
}
if (iconColor != -1) {
imageView.imageTintList = ColorStateList.valueOf(iconColor)
}
imageView.setPadding(16F.dip2px(), 0, 16F.dip2px(), 0)
return imageView
}
private fun createTextView(context: Context, isLeft: Boolean): View {
val text = if (isLeft) config.leftText else config.rightText
val textColor = if (isLeft) config.leftTextColor else config.rightTextColor
val textView = TextView(context)
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14F)
textView.setPadding(16F.dip2px(), 0, 16F.dip2px(), 0)
textView.gravity = Gravity.CENTER
textView.text = text
if (textColor == -1) {
textView.setTextColor(R.color.primary_theme.toColor(context))
} else {
textView.setTextColor(textColor)
}
return textView
}
fun setOnLeftClickListener(onClick: (View) -> Unit) {
leftView?.setOnClickListener(onClick)
}
fun setOnRightClickListener(onClick: (View) -> Unit) {
rightView?.setOnClickListener(onClick)
}
companion object {
private const val BUTTON_STYLE_TEXT = 0 // 纯文本
private const val BUTTON_STYLE_ICON = 1 // 图标
private const val BUTTON_STYLE_BUTTON = 2 // 按钮
private const val BUTTON_STYLE_TITLE = 3 // 标题
}
class Config(
val leftStyle: Int = -1,
val leftText: String = "",
val leftTextColor: Int = -1,
val leftIconResource: Int = -1,
val leftIconColor: Int = -1,
val leftButtonBackgroundResource: Int = -1,
val title: String = "",
val rightStyle: Int = -1,
val rightText: String = "",
val rightTextColor: Int = -1,
val rightIconResource: Int = -1,
val rightIconColor: Int = -1,
val rightButtonBackgroundResource: Int = -1
)
}

View File

@ -267,4 +267,29 @@
<flag name="regular" value="2" />
</attr>
</declare-styleable>
<declare-styleable name="BottomSheetTitleView">
<attr name="left_style" format="integer">
<flag name="text" value="0" />
<flag name="icon" value="1" />
<flag name="button" value="2" />
<flag name="title" value="3" />
</attr>
<attr name="left_text" format="string" />
<attr name="left_text_color" format="color|reference" />
<attr name="left_icon_resource" format="reference" />
<attr name="left_icon_color" format="color|reference" />
<attr name="left_button_background_resource" format="reference" />
<attr name="title" format="string" />
<attr name="right_style" format="integer">
<flag name="text" value="0" />
<flag name="icon" value="1" />
<flag name="button" value="2" />
</attr>
<attr name="right_text" format="string" />
<attr name="right_text_color" format="color|reference" />
<attr name="right_icon_resource" format="reference" />
<attr name="right_icon_color" format="color|reference" />
<attr name="right_button_background_resource" format="reference" />
</declare-styleable>
</resources>

View File

@ -200,6 +200,18 @@
<item name="android:textStyle">bold</item>
</style>
<!-- 正文3 -->
<style name="TextBody3">
<item name="android:textSize">13sp</item>
<item name="android:textStyle">normal</item>
</style>
<!-- 正文3 -->
<style name="TextBody3B">
<item name="android:textSize">13sp</item>
<item name="android:textStyle">bold</item>
</style>
<!-- 说明1 -->
<style name="TextCaption1">
<item name="android:textSize">12sp</item>