Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
Widget
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Android Widget
Widget
Commits
255526fa
Commit
255526fa
authored
Aug 07, 2019
by
王韦
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[new] 增加测试
parent
f20cd457
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
158 additions
and
45 deletions
+158
-45
MainActivity.kt
app/src/main/java/com/qimai/android/widget/MainActivity.kt
+33
-9
activity_main.xml
app/src/main/res/layout/activity_main.xml
+25
-2
QmTopBar.kt
.../main/java/com/qimai/android/widgetlib/topbar/QmTopBar.kt
+74
-34
TextProperty.kt
...n/java/com/qimai/android/widgetlib/topbar/TextProperty.kt
+18
-0
btn_shape.xml
widgetlib/src/main/res/drawable/btn_shape.xml
+8
-0
No files found.
app/src/main/java/com/qimai/android/widget/MainActivity.kt
View file @
255526fa
package
com.qimai.android.widget
package
com.qimai.android.widget
import
android.graphics.Color
import
android.os.Bundle
import
android.os.Bundle
import
android.widget.Button
import
android.widget.TextView
import
android.widget.Toast
import
android.widget.Toast
import
androidx.appcompat.app.AppCompatActivity
import
androidx.appcompat.app.AppCompatActivity
import
androidx.core.content.ContextCompat
import
com.qimai.android.widgetlib.topbar.QmTopBar
import
com.qimai.android.widgetlib.topbar.QmTopBar
class
MainActivity
:
AppCompatActivity
()
{
class
MainActivity
:
AppCompatActivity
()
{
...
@@ -11,17 +15,8 @@ class MainActivity : AppCompatActivity() {
...
@@ -11,17 +15,8 @@ class MainActivity : AppCompatActivity() {
super
.
onCreate
(
savedInstanceState
)
super
.
onCreate
(
savedInstanceState
)
setContentView
(
R
.
layout
.
activity_main
)
setContentView
(
R
.
layout
.
activity_main
)
val
toolbar
=
findViewById
<
QmTopBar
>(
R
.
id
.
toolbar
)
val
toolbar
=
findViewById
<
QmTopBar
>(
R
.
id
.
toolbar
)
// toolbar.run {
// title = "标题"
// subtitle = "子标题"
//// setLogo(R.drawable.abc_ic_ab_back_material)
//
//
// }
setSupportActionBar
(
toolbar
)
setSupportActionBar
(
toolbar
)
supportActionBar
?.
setDisplayShowCustomEnabled
(
false
)
supportActionBar
?.
setDisplayShowCustomEnabled
(
false
)
// toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_material)
toolbar
.
registerViewClickListener
{
toolbar
.
registerViewClickListener
{
backImgClick
{
backImgClick
{
...
@@ -39,6 +34,35 @@ class MainActivity : AppCompatActivity() {
...
@@ -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
)
}
}
}
}
}
app/src/main/res/layout/activity_main.xml
View file @
255526fa
...
@@ -11,6 +11,7 @@
...
@@ -11,6 +11,7 @@
<com.qimai.android.widgetlib.topbar.QmTopBar
<com.qimai.android.widgetlib.topbar.QmTopBar
android:id=
"@+id/toolbar"
android:id=
"@+id/toolbar"
android:layout_width=
"match_parent"
android:layout_width=
"match_parent"
app:title_text=
"详情"
android:layout_height=
"45dp"
/>
android:layout_height=
"45dp"
/>
<LinearLayout
<LinearLayout
...
@@ -18,19 +19,41 @@
...
@@ -18,19 +19,41 @@
android:layout_height=
"45dp"
>
android:layout_height=
"45dp"
>
<Button
<Button
android:id=
"@+id/line"
android:layout_width=
"wrap_content"
android:layout_width=
"wrap_content"
android:text=
"
右边img
"
android:text=
"
line
"
android:layout_height=
"45dp"
/>
android:layout_height=
"45dp"
/>
<Button
<Button
android:id=
"@+id/leftTextview"
android:layout_width=
"wrap_content"
android:layout_width=
"wrap_content"
android:text=
"右边text"
android:text=
"右边text"
android:layout_height=
"45dp"
/>
android:layout_height=
"45dp"
/>
<Button
<Button
android:id=
"@+id/center"
android:layout_width=
"wrap_content"
android:layout_width=
"wrap_content"
android:text=
"
自定义
"
android:text=
"
center
"
android:layout_height=
"45dp"
/>
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>
</LinearLayout>
...
...
widgetlib/src/main/java/com/qimai/android/widgetlib/topbar/QmTopBar.kt
View file @
255526fa
...
@@ -3,7 +3,6 @@ package com.qimai.android.widgetlib.topbar
...
@@ -3,7 +3,6 @@ package com.qimai.android.widgetlib.topbar
import
android.content.Context
import
android.content.Context
import
android.content.res.TypedArray
import
android.content.res.TypedArray
import
android.graphics.drawable.Drawable
import
android.graphics.drawable.Drawable
import
android.text.TextUtils
import
android.util.AttributeSet
import
android.util.AttributeSet
import
android.view.View
import
android.view.View
import
android.widget.ImageView
import
android.widget.ImageView
...
@@ -37,11 +36,8 @@ import com.qimai.android.widgetlib.R
...
@@ -37,11 +36,8 @@ import com.qimai.android.widgetlib.R
class
QmTopBar
:
Toolbar
{
class
QmTopBar
:
Toolbar
{
var
qmTopBarText
:
CharSequence
?
=
""
private
var
qmTopBarText
:
CharSequence
?
=
""
set
(
value
)
{
field
=
value
buildCenter
(
relativeLayout
)
}
lateinit
var
mRightImg
:
ImageView
lateinit
var
mRightImg
:
ImageView
private
set
private
set
...
@@ -54,29 +50,32 @@ class QmTopBar : Toolbar {
...
@@ -54,29 +50,32 @@ class QmTopBar : Toolbar {
lateinit
var
mRightTextView
:
TextView
lateinit
var
mRightTextView
:
TextView
private
set
private
set
lateinit
var
lineView
:
View
private
var
relativeLayout
:
RelativeLayout
=
RelativeLayout
(
context
)
private
var
relativeLayout
:
RelativeLayout
=
RelativeLayout
(
context
)
var
qmTopBarNeedSeparator
:
Boolean
=
false
var
qmTopBarNeedSeparator
:
Boolean
=
false
set
(
value
)
{
set
(
value
)
{
field
=
value
field
=
value
if
(
field
)
{
buildBottomDivideLine
(
relativeLayout
)
buildBottomDivideLine
(
relativeLayout
)
}
}
}
private
var
qmTopBarBgColor
:
Int
private
var
qmTopBarBgColor
:
Int
set
(
value
)
{
field
=
value
relativeLayout
.
setBackgroundColor
(
qmTopBarBgColor
)
}
private
var
qmTopBarTextSize
:
Int
private
var
qmTopBarTextSize
:
Int
private
var
qmTopBarTextColor
:
Int
private
var
qmTopBarTextColor
:
Int
var
qmTopBarLeftDrawable
:
Int
var
qmTopBarLeftDrawable
:
Int
set
(
value
)
{
set
(
value
)
{
field
=
value
field
=
value
buildLeftImage
(
relativeLayout
)
buildLeftImage
(
relativeLayout
)
}
}
var
qmTopBarRightDrawable
:
Int
=
-
1
var
qmTopBarRightDrawable
:
Int
=
-
1
set
(
value
)
{
set
(
value
)
{
field
=
value
field
=
value
if
(
value
!=
-
1
)
{
if
(
value
!=
-
1
)
{
...
@@ -88,11 +87,24 @@ class QmTopBar : Toolbar {
...
@@ -88,11 +87,24 @@ class QmTopBar : Toolbar {
private
var
qmTopBarRightTextSize
:
Int
private
var
qmTopBarRightTextSize
:
Int
private
var
rightTextBackground
:
Drawable
?
=
null
private
var
rightTextBackground
:
Drawable
?
=
null
private
var
qmTopBarRightText
:
CharSequence
?
=
null
private
var
qmTopBarRightText
:
CharSequence
?
=
null
var
rightTextProperty
:
TextProperty
set
(
value
)
{
set
(
value
)
{
field
=
value
field
=
value
if
(!
TextUtils
.
isEmpty
(
value
))
{
qmTopBarRightText
=
field
.
text
buildRightText
(
relativeLayout
)
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 {
...
@@ -107,6 +119,7 @@ class QmTopBar : Toolbar {
attributes
,
attributes
,
defStyleAttr
defStyleAttr
)
{
)
{
val
a
:
TypedArray
=
context
.
obtainStyledAttributes
(
val
a
:
TypedArray
=
context
.
obtainStyledAttributes
(
attributes
,
attributes
,
R
.
styleable
.
QmTopBar
,
defStyleAttr
,
0
R
.
styleable
.
QmTopBar
,
defStyleAttr
,
0
...
@@ -146,13 +159,17 @@ class QmTopBar : Toolbar {
...
@@ -146,13 +159,17 @@ class QmTopBar : Toolbar {
)
)
rightTextBackground
=
a
.
getDrawable
(
R
.
styleable
.
QmTopBar_right_text_background
)
rightTextBackground
=
a
.
getDrawable
(
R
.
styleable
.
QmTopBar_right_text_background
)
qmTopBarRightText
=
a
.
getString
(
R
.
styleable
.
QmTopBar_right_text
)
qmTopBarRightText
=
a
.
getString
(
R
.
styleable
.
QmTopBar_right_text
)
rightTextProperty
=
TextProperty
(
qmTopBarRightText
,
qmTopBarRightTextColor
,
qmTopBarRightTextSize
,
rightTextBackground
)
centerTextProperty
=
TextProperty
(
qmTopBarText
,
qmTopBarTextColor
,
qmTopBarTextSize
,
null
)
a
.
recycle
()
a
.
recycle
()
}
}
/**
/**
* 自定义 左边的返回按钮view
* 自定义 左边的返回按钮view
* 移除自带的,直接加入用户的
* 移除自带的,直接加入用户的
*
@param params = null 默认 垂直居中 居左 离左边 16dp 宽 搞 wrap_content
*
@param params = null 默认配置: (垂直居中 居左 离左边 16dp 宽/高 wrap_content)
*/
*/
fun
customLeftView
(
view
:
View
,
params
:
RelativeLayout
.
LayoutParams
?)
{
fun
customLeftView
(
view
:
View
,
params
:
RelativeLayout
.
LayoutParams
?)
{
if
(
params
==
null
)
{
if
(
params
==
null
)
{
...
@@ -164,11 +181,17 @@ class QmTopBar : Toolbar {
...
@@ -164,11 +181,17 @@ class QmTopBar : Toolbar {
leftImageViewParams
.
addRule
(
RelativeLayout
.
CENTER_VERTICAL
)
leftImageViewParams
.
addRule
(
RelativeLayout
.
CENTER_VERTICAL
)
leftImageViewParams
.
leftMargin
=
dipTopx
(
context
,
16
)
leftImageViewParams
.
leftMargin
=
dipTopx
(
context
,
16
)
relativeLayout
.
addView
(
view
,
left
)
relativeLayout
.
addView
(
view
,
leftImageViewParams
)
relativeLayout
.
removeView
(
leftImageView
)
return
return
}
}
relativeLayout
.
addView
(
view
,
params
)
relativeLayout
.
removeView
(
leftImageView
)
relativeLayout
.
removeView
(
leftImageView
)
relativeLayout
.
addView
(
view
,
params
)
}
fun
customeRightView
(
view
:
View
,
params
:
RelativeLayout
.
LayoutParams
?)
{
}
}
...
@@ -185,6 +208,7 @@ class QmTopBar : Toolbar {
...
@@ -185,6 +208,7 @@ class QmTopBar : Toolbar {
rightImgParams
.
addRule
(
RelativeLayout
.
ALIGN_PARENT_RIGHT
)
rightImgParams
.
addRule
(
RelativeLayout
.
ALIGN_PARENT_RIGHT
)
rightImgParams
.
rightMargin
=
dipTopx
(
context
,
16
)
rightImgParams
.
rightMargin
=
dipTopx
(
context
,
16
)
relativeLayout
.
addView
(
mRightImg
,
rightImgParams
)
relativeLayout
.
addView
(
mRightImg
,
rightImgParams
)
registerRightImgListener
()
}
}
}
}
...
@@ -208,9 +232,13 @@ class QmTopBar : Toolbar {
...
@@ -208,9 +232,13 @@ class QmTopBar : Toolbar {
rightTextParams
.
addRule
(
RelativeLayout
.
CENTER_VERTICAL
)
rightTextParams
.
addRule
(
RelativeLayout
.
CENTER_VERTICAL
)
}
else
{
}
else
{
rightTextParams
.
addRule
(
RelativeLayout
.
CENTER_VERTICAL
)
rightTextParams
.
addRule
(
RelativeLayout
.
CENTER_VERTICAL
)
rightTextParams
.
marginEnd
=
dipTopx
(
context
,
16
)
rightTextParams
.
addRule
(
RelativeLayout
.
ALIGN_PARENT_RIGHT
)
rightTextParams
.
addRule
(
RelativeLayout
.
ALIGN_PARENT_RIGHT
)
}
}
mRightTextView
.
setPadding
(
15
,
5
,
15
,
5
)
relativeLayout
.
addView
(
mRightTextView
,
rightTextParams
)
relativeLayout
.
addView
(
mRightTextView
,
rightTextParams
)
registerRightTextListener
()
}
}
fun
registerViewClickListener
(
listenerViewClick
:
ListenerViewClick
.()
->
Unit
)
{
fun
registerViewClickListener
(
listenerViewClick
:
ListenerViewClick
.()
->
Unit
)
{
...
@@ -258,11 +286,18 @@ class QmTopBar : Toolbar {
...
@@ -258,11 +286,18 @@ class QmTopBar : Toolbar {
private
fun
buildBottomDivideLine
(
relativeLayout
:
RelativeLayout
)
{
private
fun
buildBottomDivideLine
(
relativeLayout
:
RelativeLayout
)
{
// 下划线
// 下划线
val
lineView
=
View
(
context
)
if
(
qmTopBarNeedSeparator
&&
!
::
lineView
.
isInitialized
)
{
val
lineParams
=
RelativeLayout
.
LayoutParams
(
RelativeLayout
.
LayoutParams
.
MATCH_PARENT
,
dipTopx
(
context
,
1
))
lineView
=
View
(
context
)
lineView
.
setBackgroundColor
(
ContextCompat
.
getColor
(
context
,
R
.
color
.
QmTopBar_color_999999
))
val
lineParams
=
RelativeLayout
.
LayoutParams
(
RelativeLayout
.
LayoutParams
.
MATCH_PARENT
,
dipTopx
(
context
,
1
))
lineParams
.
addRule
(
RelativeLayout
.
ALIGN_PARENT_BOTTOM
)
lineView
.
setBackgroundColor
(
ContextCompat
.
getColor
(
context
,
R
.
color
.
QmTopBar_color_999999
))
relativeLayout
.
addView
(
lineView
,
lineParams
)
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 {
...
@@ -281,20 +316,25 @@ class QmTopBar : Toolbar {
relativeLayout
.
addView
(
leftImageView
,
leftImageViewParams
)
relativeLayout
.
addView
(
leftImageView
,
leftImageViewParams
)
}
}
private
fun
buildCenter
(
relativeLayout
:
RelativeLayout
)
{
private
fun
build
OrModify
Center
(
relativeLayout
:
RelativeLayout
)
{
if
(!
::
centerTextView
.
isInitialized
)
if
(!
::
centerTextView
.
isInitialized
)
{
centerTextView
=
TextView
(
context
)
centerTextView
=
TextView
(
context
)
val
centerViewParams
=
RelativeLayout
.
LayoutParams
(
val
centerViewParams
=
RelativeLayout
.
LayoutParams
(
RelativeLayout
.
LayoutParams
.
WRAP_CONTENT
,
RelativeLayout
.
LayoutParams
.
WRAP_CONTENT
,
RelativeLayout
.
LayoutParams
.
WRAP_CONTENT
RelativeLayout
.
LayoutParams
.
WRAP_CONTENT
)
)
centerViewParams
.
addRule
(
RelativeLayout
.
CENTER_IN_PARENT
)
centerViewParams
.
addRule
(
RelativeLayout
.
CENTER_IN_PARENT
)
centerTextView
.
text
=
qmTopBarText
centerTextView
.
text
=
qmTopBarText
centerTextView
.
setTextColor
(
qmTopBarTextColor
)
centerTextView
.
setTextColor
(
qmTopBarTextColor
)
centerTextView
.
id
=
View
.
generateViewId
()
centerTextView
.
id
=
View
.
generateViewId
()
relativeLayout
.
addView
(
centerTextView
,
centerViewParams
)
relativeLayout
.
addView
(
centerTextView
,
centerViewParams
)
}
else
{
centerTextView
.
text
=
qmTopBarText
centerTextView
.
setTextColor
(
qmTopBarTextColor
)
}
}
}
inner
class
ListenerViewClick
{
inner
class
ListenerViewClick
{
...
...
widgetlib/src/main/java/com/qimai/android/widgetlib/topbar/TextProperty.kt
0 → 100644
View file @
255526fa
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
widgetlib/src/main/res/drawable/btn_shape.xml
0 → 100644
View file @
255526fa
<?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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment