Skip to content

Commit fb12406

Browse files
committed
升级material版本,解决高版本反射崩溃问题
1 parent 73e4d46 commit fb12406

File tree

2 files changed

+14
-32
lines changed

2 files changed

+14
-32
lines changed

utils/src/main/java/com/catchpig/utils/ext/SnackbarExt.kt

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.catchpig.utils.ext
22

3-
import android.graphics.Rect
43
import android.view.Gravity
54
import android.view.View
5+
import android.view.ViewGroup.MarginLayoutParams
66
import android.widget.FrameLayout
77
import android.widget.TextView
88
import androidx.annotation.ColorRes
@@ -13,17 +13,20 @@ import androidx.core.view.updateLayoutParams
1313
import com.catchpig.utils.R
1414
import com.google.android.material.snackbar.Snackbar
1515

16-
fun View.showSnackBar(text: CharSequence, @DrawableRes background: Int, gravity: Int = Gravity.BOTTOM) {
16+
fun View.showSnackBar(
17+
text: CharSequence,
18+
@DrawableRes background: Int,
19+
gravity: Int = Gravity.BOTTOM
20+
) {
1721
val snackbar = Snackbar.make(this, text, Snackbar.LENGTH_LONG)
1822
snackbar.setBackgroundResource(background)
1923
snackbar.setTextColorRes(R.color.color_black)
2024
if (gravity == Gravity.BOTTOM) {
21-
val parentView = snackbar.targetParent()
22-
val bottom = 20 + context.px2dp((parentView.bottom - bottom))
23-
snackbar.setMargin(20, 0, 20, bottom)
25+
val layoutParams = snackbar.view.layoutParams as MarginLayoutParams
26+
layoutParams.bottomMargin = dp2px(20)
27+
snackbar.view.layoutParams = layoutParams
2428
} else {
2529
snackbar.setGravity(gravity)
26-
snackbar.setMargin(20, 0, 20, 0)
2730
}
2831
snackbar.show()
2932
}
@@ -37,12 +40,11 @@ fun View.showSnackBar(
3740
snackbar.setBackgroundResource(background)
3841
snackbar.setTextColorRes(R.color.color_black)
3942
if (gravity == Gravity.BOTTOM) {
40-
val parentView = snackbar.targetParent()
41-
val bottom = 20 + context.px2dp((parentView.bottom - bottom))
42-
snackbar.setMargin(20, 0, 20, bottom)
43+
val layoutParams = snackbar.view.layoutParams as MarginLayoutParams
44+
layoutParams.bottomMargin = dp2px(20)
45+
snackbar.view.layoutParams = layoutParams
4346
} else {
4447
snackbar.setGravity(gravity)
45-
snackbar.setMargin(20, 0, 20, 0)
4648
}
4749
snackbar.show()
4850
}
@@ -73,30 +75,10 @@ fun Snackbar.setGravity(gravity: Int) {
7375
is FrameLayout.LayoutParams -> {
7476
this.gravity = gravity
7577
}
78+
7679
is CoordinatorLayout.LayoutParams -> {
7780
this.gravity = gravity
7881
}
7982
}
8083
}
81-
}
82-
83-
fun Snackbar.targetParent(): View {
84-
val cls = this::class.java.superclass
85-
val field = cls.getDeclaredField("targetParent")
86-
field.isAccessible = true
87-
return field.get(this) as View
88-
}
89-
90-
/**
91-
* 设置外边框
92-
*/
93-
fun Snackbar.setMargin(startDp: Int, topDp: Int, endDp: Int, bottomDp: Int) {
94-
val cls = this::class.java.superclass
95-
val field = cls.getDeclaredField("originalMargins")
96-
field.isAccessible = true
97-
val marginStart = context.dp2px(startDp)
98-
val topMargin = context.dp2px(topDp)
99-
val marginEnd = context.dp2px(endDp)
100-
val bottomMargin = context.dp2px(bottomDp)
101-
field.set(this, Rect(marginStart, topMargin, marginEnd, bottomMargin))
10284
}

version.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ ext {
2525
'coordinatorlayout' : '1.2.0',
2626
'constraintlayout' : '2.1.3',
2727
'annotation' : '1.3.0',
28-
'material' : '1.5.0',
28+
'material' : '1.10.0',
2929
'core_ktx' : '1.8.0',
3030
'okhttp3' : '4.10.0',
3131
'retrofit2' : '2.9.0',

0 commit comments

Comments
 (0)