Skip to content

Commit f1e2371

Browse files
committed
增加对齐方式
1 parent 5d8bde2 commit f1e2371

File tree

8 files changed

+82
-16
lines changed

8 files changed

+82
-16
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ allprojects {
1111
}
1212
1313
dependencies {
14-
implementation 'com.github.LuckyCodeer:TagLayout:1.0.9'
14+
implementation 'com.github.LuckyCodeer:TagLayout:1.1.0'
1515
}
1616
```
1717

@@ -54,13 +54,15 @@ dependencies {
5454
android:layout_width="wrap_content"
5555
android:layout_height="wrap_content"
5656
app:defaultChoicePosition="0"
57-
app:choiceMode="none" />
57+
app:choiceMode="none"
58+
app:gravity="left"/>
5859
```
5960

6061
##### 布局属性
6162
1. app:defaultChoicePosition="0" //单选时默认选中项
6263
2. app:choiceMode="none" //设置选择模式,支持单选(singleChoice)和多选(multipleChoice) 默认(none)表示不设置选择模式
6364
3. app:singleChoiceSupportCancel="false" //单选支持取消已选中项,默认为false
65+
4. app:gravity="left" //对齐方式,支持 left、center、right,默认为left
6466

6567

6668
#### 动态添加数据

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ android {
1111
applicationId "com.yhw.taglayout.sample"
1212
minSdkVersion 16
1313
targetSdkVersion 30
14-
versionCode 1
15-
versionName "1.0"
14+
versionCode 110
15+
versionName "1.1.0"
1616

1717
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1818
}

app/src/main/res/layout/activity_main.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<com.yhw.taglayout.TagLayout
1111
android:id="@+id/tag_layout_1"
12-
android:layout_width="wrap_content"
12+
android:layout_width="match_parent"
1313
android:layout_height="wrap_content"
1414
app:choiceMode="singleChoice">
1515

@@ -43,6 +43,7 @@
4343
android:id="@+id/tag_layout_2"
4444
android:layout_width="match_parent"
4545
android:layout_height="wrap_content"
46+
app:gravity="left"
4647
app:choiceMode="none"
4748
app:defaultChoicePosition="0"
4849
app:singleChoiceSupportCancel="true"/>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Wed Sep 06 15:10:03 CST 2023
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
4-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
4+
distributionUrl=https\://mirrors.cloud.tencent.com/gradle/gradle-7.2-bin.zip
55
zipStoreBase=GRADLE_USER_HOME
66
zipStorePath=wrapper/dists

library/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ android {
99
defaultConfig {
1010
minSdkVersion 16
1111
targetSdkVersion 30
12-
versionCode 109
13-
versionName "1.0.9"
12+
versionCode 110
13+
versionName "1.1.0"
1414

1515
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1616
consumerProguardFiles "consumer-rules.pro"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.yhw.taglayout
2+
3+
import java.io.Serializable
4+
5+
/**
6+
* created by yhw
7+
* date 2024/8/7
8+
*/
9+
class MyRect(var left: Int, var top: Int, var right: Int, var bottom: Int) : Serializable {
10+
var lineNum = 0 //行数
11+
}

library/src/main/java/com/yhw/taglayout/TagLayout.kt

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.yhw.taglayout
22

33
import android.annotation.SuppressLint
44
import android.content.Context
5-
import android.graphics.Rect
65
import android.util.AttributeSet
76
import android.util.DisplayMetrics
87
import android.util.Log
@@ -18,7 +17,8 @@ private const val TAG = "TagLayout===>"
1817
* @author yhw
1918
*/
2019
class TagLayout : ViewGroup {
21-
private val mViewRectMap = mutableMapOf<View, Rect>()
20+
private val mViewRectMap = mutableMapOf<View, MyRect>()
21+
private val mLineWidthMap = mutableMapOf<Int, Int>() //存放行数和距离右侧的宽度
2222
private var choiceMode = ChoiceMode.None.choiceMode //选择模式
2323
private var defChoicePosition: Int = 0 //单选时默认选中
2424
private lateinit var mAdapter: TagAdapter
@@ -28,6 +28,8 @@ class TagLayout : ViewGroup {
2828
var onMultipleCheckedChangeListener: OnMultipleCheckedChangeListener? = null
2929
private var mScreenWidth = 0 //屏幕宽度
3030
private var mSingleChoiceSupportCancel = false //单选是否支持取消
31+
private var mMeasuredWidth = 0 //控件宽度
32+
private var mGravity = 0 //对齐方式
3133

3234
constructor(context: Context) : this(context, null)
3335
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
@@ -41,6 +43,7 @@ class TagLayout : ViewGroup {
4143
defChoicePosition = ta.getInt(R.styleable.TagLayout_defaultChoicePosition, 0)
4244
mSingleChoiceSupportCancel =
4345
ta.getBoolean(R.styleable.TagLayout_singleChoiceSupportCancel, false)
46+
mGravity = ta.getInt(R.styleable.TagLayout_gravity, GravityMode.Left.gravity)
4447
ta.recycle()
4548

4649
val windowManager = context
@@ -63,6 +66,7 @@ class TagLayout : ViewGroup {
6366
var lineWidth = 0 //行宽
6467
var lineHeight = 0 //行高
6568
Log.i(TAG, "widthSize is $widthSize widthMode is $widthMode mScreenWidth:${mScreenWidth}")
69+
var lineNum = 0 //行数
6670
measureChildren(widthMeasureSpec, heightMeasureSpec)
6771
for (i in 0 until childCount) {
6872
val childView = getChildAt(i)
@@ -77,6 +81,7 @@ class TagLayout : ViewGroup {
7781

7882
//判断是否需要换行
7983
if (lineWidth + childWidth > widthSize - paddingLeft - paddingRight) {
84+
lineNum ++
8085
//记录所有行中宽度最大的行
8186
width = max(width, lineWidth)
8287
//换行后重置行宽为第一个view的宽度
@@ -91,7 +96,9 @@ class TagLayout : ViewGroup {
9196
//记录每行view中高度最高的view为当前行高
9297
lineHeight = max(childHeight, lineHeight)
9398
}
94-
99+
Log.i(TAG, "lineCount============ $lineNum")
100+
val srcLineWidth = if (mLineWidthMap[lineNum] != null) mLineWidthMap[lineNum]!! else 0
101+
mLineWidthMap[lineNum] = max(lineWidth, srcLineWidth)
95102
if (choiceMode == ChoiceMode.SingleChoice.choiceMode) {
96103
if (i in 0 until childCount && i == defChoicePosition)
97104
childView.isSelected = true
@@ -105,7 +112,8 @@ class TagLayout : ViewGroup {
105112
val childTop = height + marginLayoutParams.topMargin + paddingTop
106113
val childBottom =
107114
height + childHeight - marginLayoutParams.bottomMargin + paddingTop
108-
val rect = Rect(childLeft, childTop, childRight, childBottom)
115+
val rect = MyRect(childLeft, childTop, childRight, childBottom)
116+
rect.lineNum = lineNum
109117
Log.i(
110118
TAG, "onMeasure left:${rect.left} top:${rect.top} ," +
111119
"right:${rect.right} ,bottom:${rect.bottom} measuredWidth:${childView.measuredWidth}"
@@ -119,22 +127,34 @@ class TagLayout : ViewGroup {
119127
height += lineHeight
120128
}
121129
}
122-
val measuredWidth =
130+
mMeasuredWidth =
123131
if (widthMode == MeasureSpec.EXACTLY) widthSize else width + paddingLeft + paddingRight
124132
val measuredHeight =
125133
if (heightMode == MeasureSpec.EXACTLY) heightSize else height + paddingTop + paddingBottom
126134
Log.i(
127135
TAG,
128-
"measuredWidth :${measuredWidth} measuredHeight:${measuredHeight} width:${width} lineWidth:${lineWidth}"
136+
"measuredWidth :${mMeasuredWidth} measuredHeight:${measuredHeight} width:${width} lineWidth:${lineWidth}"
129137
)
130-
setMeasuredDimension(max(measuredWidth, width), measuredHeight)
138+
139+
setMeasuredDimension(max(mMeasuredWidth, width), measuredHeight)
131140
}
132141

133142
override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
134143
Log.i(TAG, "=========onLayout========== $changed $l $t $r $b")
135144
var i = 0
136145
for ((childView, rect) in mViewRectMap) {
137-
childView.layout(rect.left, rect.top, rect.right, rect.bottom)
146+
val lineRightWidth = mLineWidthMap[rect.lineNum]
147+
var moveGap = 0
148+
if (mGravity == GravityMode.RIGHT.gravity) {
149+
if (lineRightWidth != null && lineRightWidth > 0) {
150+
moveGap = mMeasuredWidth - lineRightWidth
151+
}
152+
} else if (mGravity == GravityMode.CENTER.gravity) {
153+
if (lineRightWidth != null && lineRightWidth > 0) {
154+
moveGap = (mMeasuredWidth - lineRightWidth) / 2
155+
}
156+
}
157+
childView.layout(rect.left + moveGap, rect.top, rect.right + moveGap, rect.bottom)
138158
setItemListener(childView, i)
139159
i++
140160
}
@@ -322,6 +342,9 @@ class TagLayout : ViewGroup {
322342
fun onCheckedChanged(positionList: MutableList<Int>)
323343
}
324344

345+
/**
346+
* 选择模式
347+
*/
325348
enum class ChoiceMode(var choiceMode: Int) {
326349
/**
327350
* 非选择模式
@@ -338,4 +361,24 @@ class TagLayout : ViewGroup {
338361
*/
339362
MultipleChoice(2);
340363
}
364+
365+
/**
366+
* 对齐方式
367+
*/
368+
enum class GravityMode(var gravity: Int) {
369+
/**
370+
* 居左对齐
371+
*/
372+
Left(0),
373+
374+
/**
375+
* 居中对齐
376+
*/
377+
CENTER(1),
378+
379+
/**
380+
* 居右对齐
381+
*/
382+
RIGHT(2);
383+
}
341384
}

library/src/main/res/values/attrs.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,14 @@
1414
<attr name="defaultChoicePosition" format="integer"/>
1515
<!--单选支持取消-->
1616
<attr name="singleChoiceSupportCancel" format="boolean"/>
17+
<!--对齐方式-->
18+
<attr name="gravity" format="enum">
19+
<!--居左对齐默认-->
20+
<enum name="left" value="0" />
21+
<!-- 居中对齐 -->
22+
<enum name="center" value="1" />
23+
<!-- 居右对齐 -->
24+
<enum name="right" value="2" />
25+
</attr>
1726
</declare-styleable>
1827
</resources>

0 commit comments

Comments
 (0)