Commit 255526fa authored by 王韦's avatar 王韦

[new] 增加测试

parent f20cd457
package com.qimai.android.widget
import android.graphics.Color
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.qimai.android.widgetlib.topbar.QmTopBar
class MainActivity : AppCompatActivity() {
......@@ -11,17 +15,8 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toolbar = findViewById<QmTopBar>(R.id.toolbar)
// toolbar.run {
// title = "标题"
// subtitle = "子标题"
//// setLogo(R.drawable.abc_ic_ab_back_material)
//
//
// }
setSupportActionBar(toolbar)
supportActionBar?.setDisplayShowCustomEnabled(false)
// toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_material)
toolbar.registerViewClickListener {
backImgClick {
......@@ -39,6 +34,35 @@ class MainActivity : AppCompatActivity() {
}
findViewById<Button>(R.id.line).setOnClickListener {
toolbar.qmTopBarNeedSeparator = !toolbar.qmTopBarNeedSeparator
}
findViewById<Button>(R.id.leftTextview).setOnClickListener {
toolbar.rightTextProperty = toolbar.rightTextProperty.apply {
text = "确定"
textColor = ContextCompat.getColor(this@MainActivity, R.color.abc_btn_colored_borderless_text_material)
textBackground = ContextCompat.getDrawable(this@MainActivity, R.drawable.btn_shape)
}
}
findViewById<Button>(R.id.center).setOnClickListener {
toolbar.centerTextProperty = toolbar.centerTextProperty.apply {
text = "修改"
textColor = ContextCompat.getColor(this@MainActivity, R.color.abc_btn_colored_borderless_text_material)
}
}
findViewById<Button>(R.id.customleft).setOnClickListener {
var text = TextView(this)
text.text = "取消"
text.setTextColor(Color.RED)
text.textSize = 16f
toolbar.customLeftView(text, null)
}
}
}
......@@ -11,6 +11,7 @@
<com.qimai.android.widgetlib.topbar.QmTopBar
android:id="@+id/toolbar"
android:layout_width="match_parent"
app:title_text="详情"
android:layout_height="45dp"/>
<LinearLayout
......@@ -18,19 +19,41 @@
android:layout_height="45dp">
<Button
android:id="@+id/line"
android:layout_width="wrap_content"
android:text="右边img"
android:text="line"
android:layout_height="45dp"/>
<Button
android:id="@+id/leftTextview"
android:layout_width="wrap_content"
android:text="右边text"
android:layout_height="45dp"/>
<Button
android:id="@+id/center"
android:layout_width="wrap_content"
android:text="自定义"
android:text="center"
android:layout_height="45dp"/>
<Button
android:id="@+id/customleft"
android:layout_width="wrap_content"
android:text="自定义左边"
android:layout_height="45dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<Button
android:id="@+id/customright"
android:layout_width="wrap_content"
android:text="自定义右边"
android:layout_height="45dp"/>
</LinearLayout>
......
......@@ -3,7 +3,6 @@ package com.qimai.android.widgetlib.topbar
import android.content.Context
import android.content.res.TypedArray
import android.graphics.drawable.Drawable
import android.text.TextUtils
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
......@@ -37,11 +36,8 @@ import com.qimai.android.widgetlib.R
class QmTopBar : Toolbar {
var qmTopBarText: CharSequence? = ""
set(value) {
field = value
buildCenter(relativeLayout)
}
private var qmTopBarText: CharSequence? = ""
lateinit var mRightImg: ImageView
private set
......@@ -54,29 +50,32 @@ class QmTopBar : Toolbar {
lateinit var mRightTextView: TextView
private set
lateinit var lineView: View
private var relativeLayout: RelativeLayout = RelativeLayout(context)
var qmTopBarNeedSeparator: Boolean = false
var qmTopBarNeedSeparator: Boolean = false
set(value) {
field = value
if (field) {
buildBottomDivideLine(relativeLayout)
}
buildBottomDivideLine(relativeLayout)
}
private var qmTopBarBgColor: Int
set(value) {
field = value
relativeLayout.setBackgroundColor(qmTopBarBgColor)
}
private var qmTopBarTextSize: Int
private var qmTopBarTextColor: Int
var qmTopBarLeftDrawable: Int
var qmTopBarLeftDrawable: Int
set(value) {
field = value
buildLeftImage(relativeLayout)
}
var qmTopBarRightDrawable: Int = -1
var qmTopBarRightDrawable: Int = -1
set(value) {
field = value
if (value != -1) {
......@@ -88,11 +87,24 @@ class QmTopBar : Toolbar {
private var qmTopBarRightTextSize: Int
private var rightTextBackground: Drawable? = null
private var qmTopBarRightText: CharSequence? = null
var rightTextProperty: TextProperty
set(value) {
field = value
if (!TextUtils.isEmpty(value)) {
buildRightText(relativeLayout)
}
qmTopBarRightText = field.text
qmTopBarRightTextSize = field.textSize
qmTopBarRightTextColor = field.textColor
rightTextBackground = field.textBackground
buildRightText(relativeLayout)
}
var centerTextProperty: TextProperty
set(value) {
field = value
qmTopBarText = field.text
qmTopBarTextColor = field.textColor
qmTopBarTextSize = field.textSize
buildOrModifyCenter(relativeLayout)
}
......@@ -107,6 +119,7 @@ class QmTopBar : Toolbar {
attributes,
defStyleAttr
) {
val a: TypedArray = context.obtainStyledAttributes(
attributes,
R.styleable.QmTopBar, defStyleAttr, 0
......@@ -146,13 +159,17 @@ class QmTopBar : Toolbar {
)
rightTextBackground = a.getDrawable(R.styleable.QmTopBar_right_text_background)
qmTopBarRightText = a.getString(R.styleable.QmTopBar_right_text)
rightTextProperty =
TextProperty(qmTopBarRightText, qmTopBarRightTextColor, qmTopBarRightTextSize, rightTextBackground)
centerTextProperty = TextProperty(qmTopBarText, qmTopBarTextColor, qmTopBarTextSize, null)
a.recycle()
}
/**
* 自定义 左边的返回按钮view
* 移除自带的,直接加入用户的
*@param params = null 默认 垂直居中 居左 离左边 16dp 宽 搞 wrap_content
* @param params = null 默认配置: (垂直居中 居左 离左边 16dp 宽/高 wrap_content)
*/
fun customLeftView(view: View, params: RelativeLayout.LayoutParams?) {
if (params == null) {
......@@ -164,11 +181,17 @@ class QmTopBar : Toolbar {
leftImageViewParams.addRule(RelativeLayout.CENTER_VERTICAL)
leftImageViewParams.leftMargin = dipTopx(context, 16)
relativeLayout.addView(view, left)
relativeLayout.addView(view, leftImageViewParams)
relativeLayout.removeView(leftImageView)
return
}
relativeLayout.addView(view, params)
relativeLayout.removeView(leftImageView)
relativeLayout.addView(view, params)
}
fun customeRightView(view: View, params: RelativeLayout.LayoutParams?) {
}
......@@ -185,6 +208,7 @@ class QmTopBar : Toolbar {
rightImgParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
rightImgParams.rightMargin = dipTopx(context, 16)
relativeLayout.addView(mRightImg, rightImgParams)
registerRightImgListener()
}
}
......@@ -208,9 +232,13 @@ class QmTopBar : Toolbar {
rightTextParams.addRule(RelativeLayout.CENTER_VERTICAL)
} else {
rightTextParams.addRule(RelativeLayout.CENTER_VERTICAL)
rightTextParams.marginEnd = dipTopx(context, 16)
rightTextParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
}
mRightTextView.setPadding(15, 5, 15, 5)
relativeLayout.addView(mRightTextView, rightTextParams)
registerRightTextListener()
}
fun registerViewClickListener(listenerViewClick: ListenerViewClick.() -> Unit) {
......@@ -258,11 +286,18 @@ class QmTopBar : Toolbar {
private fun buildBottomDivideLine(relativeLayout: RelativeLayout) {
// 下划线
val lineView = View(context)
val lineParams = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, dipTopx(context, 1))
lineView.setBackgroundColor(ContextCompat.getColor(context, R.color.QmTopBar_color_999999))
lineParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)
relativeLayout.addView(lineView, lineParams)
if (qmTopBarNeedSeparator && !::lineView.isInitialized) {
lineView = View(context)
val lineParams = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, dipTopx(context, 1))
lineView.setBackgroundColor(ContextCompat.getColor(context, R.color.QmTopBar_color_999999))
lineParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)
relativeLayout.addView(lineView, lineParams)
} else if (!qmTopBarNeedSeparator && ::lineView.isInitialized) {
lineView.visibility = View.GONE
} else if (qmTopBarNeedSeparator && ::lineView.isInitialized) {
lineView.visibility = View.VISIBLE
}
}
......@@ -281,20 +316,25 @@ class QmTopBar : Toolbar {
relativeLayout.addView(leftImageView, leftImageViewParams)
}
private fun buildCenter(relativeLayout: RelativeLayout) {
private fun buildOrModifyCenter(relativeLayout: RelativeLayout) {
if (!::centerTextView.isInitialized)
if (!::centerTextView.isInitialized) {
centerTextView = TextView(context)
val centerViewParams = RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
)
centerViewParams.addRule(RelativeLayout.CENTER_IN_PARENT)
val centerViewParams = RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
)
centerViewParams.addRule(RelativeLayout.CENTER_IN_PARENT)
centerTextView.text = qmTopBarText
centerTextView.setTextColor(qmTopBarTextColor)
centerTextView.id = View.generateViewId()
relativeLayout.addView(centerTextView, centerViewParams)
centerTextView.text = qmTopBarText
centerTextView.setTextColor(qmTopBarTextColor)
centerTextView.id = View.generateViewId()
relativeLayout.addView(centerTextView, centerViewParams)
} else {
centerTextView.text = qmTopBarText
centerTextView.setTextColor(qmTopBarTextColor)
}
}
inner class ListenerViewClick {
......
package com.qimai.android.widgetlib.topbar
import android.graphics.drawable.Drawable
/**
@author wangwei
@version 1.1.1
@date 2019-08-07
@Description
@email wangwei_5521@163.com
**/
data class TextProperty(
var text: CharSequence?,
var textColor: Int,
var textSize: Int,
var textBackground: Drawable?
)
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="4dp"/>
<solid android:color="@color/QmTopBar_color_FFFFFFF"/>
<stroke
android:color="@color/QmTopBar_color_3333333"
android:width="1dp"/>
</shape>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment