获取 TextInputLayout 中轮廓框的高度
Get height of outline box in TextInputLayout
-
android
-
android-textinputlayout
-
android-textinputedittext
-
material-components
-
material-components-android
我想要什么:
我正在构建一个如下所示的组件,其中 +
和 -
按钮的高度需要与 TextInputLayout 的轮廓高度相匹配。
我试过的:
好像没有publicapi可以让我得到轮廓的高度。
但是,在 TextInputLayout
中有 private MaterialShapeDrawable boxBackground;
但它是私有的。
如何使我的按钮与轮廓的高度匹配?
我建议您从 google material guidelines 开始查找默认大小。
以下不会得到轮廓的高度,但无论如何它都会对齐,因为我刚才用过类似的东西。
在您的组件中,您可以将 GlobalLayoutListener 添加到 init 中的 viewTreeObserver 或任何初始化组件的地方,这样您就可以获得正确的 TextInputLayout 大小,否则您将始终获得 0。
在 OnGlobalLayout() 方法中,您必须进行一些计算。
- 您必须找出 TextInputLayout 中的 EditText 的大小。
- 随着 EditText 的大小,您必须增加按钮的 Height/Width
- 通过使用文档,我假设 Error/HelperText 可见时总是会将小部件的大小增加 16。
- 因此获取 textInputLayout 的测量大小并减去 EditText 的大小。那应该是你的按钮新的顶部填充,但是因为当 Error/HelperText 可见时它会改变小部件的大小并使你的布局不对齐,所以你可以检查 TextInputLayout helperText/error 是否为空或为空并使用根据上面的假设,当这些视图可见时,您可以减去 16dp,它会将视图向下推,与 EditText 正确对齐,而不是与 Hint 对齐。
例如
Init 或任何初始化它的地方
init {
viewTreeObserver.addOnGlobalLayoutListener(object :
ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
textInputLayout.editText?.measuredHeight?.let {
val topMargin = 16.dpToPx() //Mentioned in the step #3
val value = if (!textInputLayout.helperText.isNullOrEmpty()) {
(textInputLayout.measuredHeight - topMargin) - it
} else {
textInputLayout.measuredHeight - it
}
increaseButtonSize(it, value)
}
viewTreeObserver.removeOnGlobalLayoutListener(this)
}
})
}
增加按钮的大小
fun increaseButtonSize(size: Int, padding: Int) {
val lessButtonParams: LayoutParams = lessButton.layoutParams as LayoutParams
lessButtonParams.setMargins(lessButtonParams.leftMargin, padding, lessButtonParams.rightMargin, lessButtonParams.bottomMargin)
lessButtonParams.height = size
lessButton.layoutParams = lessButtonParams
val moreButtonParams: LayoutParams = moreButton.layoutParams as LayoutParams
params.setMargins(moreButtonParams.leftMargin, padding, moreButtonParams.rightMargin, moreButtonParams.bottomMargin)
params.height = size
moreButton.layoutParams = params
}
android
android-textinputlayout
android-textinputedittext
material-components
material-components-android
我想要什么:
我正在构建一个如下所示的组件,其中 +
和 -
按钮的高度需要与 TextInputLayout 的轮廓高度相匹配。
我试过的:
好像没有publicapi可以让我得到轮廓的高度。
但是,在 TextInputLayout
中有 private MaterialShapeDrawable boxBackground;
但它是私有的。
如何使我的按钮与轮廓的高度匹配?
我建议您从 google material guidelines 开始查找默认大小。
以下不会得到轮廓的高度,但无论如何它都会对齐,因为我刚才用过类似的东西。
在您的组件中,您可以将 GlobalLayoutListener 添加到 init 中的 viewTreeObserver 或任何初始化组件的地方,这样您就可以获得正确的 TextInputLayout 大小,否则您将始终获得 0。 在 OnGlobalLayout() 方法中,您必须进行一些计算。
- 您必须找出 TextInputLayout 中的 EditText 的大小。
- 随着 EditText 的大小,您必须增加按钮的 Height/Width
- 通过使用文档,我假设 Error/HelperText 可见时总是会将小部件的大小增加 16。
- 因此获取 textInputLayout 的测量大小并减去 EditText 的大小。那应该是你的按钮新的顶部填充,但是因为当 Error/HelperText 可见时它会改变小部件的大小并使你的布局不对齐,所以你可以检查 TextInputLayout helperText/error 是否为空或为空并使用根据上面的假设,当这些视图可见时,您可以减去 16dp,它会将视图向下推,与 EditText 正确对齐,而不是与 Hint 对齐。
例如
Init 或任何初始化它的地方
init {
viewTreeObserver.addOnGlobalLayoutListener(object :
ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
textInputLayout.editText?.measuredHeight?.let {
val topMargin = 16.dpToPx() //Mentioned in the step #3
val value = if (!textInputLayout.helperText.isNullOrEmpty()) {
(textInputLayout.measuredHeight - topMargin) - it
} else {
textInputLayout.measuredHeight - it
}
increaseButtonSize(it, value)
}
viewTreeObserver.removeOnGlobalLayoutListener(this)
}
})
}
增加按钮的大小
fun increaseButtonSize(size: Int, padding: Int) {
val lessButtonParams: LayoutParams = lessButton.layoutParams as LayoutParams
lessButtonParams.setMargins(lessButtonParams.leftMargin, padding, lessButtonParams.rightMargin, lessButtonParams.bottomMargin)
lessButtonParams.height = size
lessButton.layoutParams = lessButtonParams
val moreButtonParams: LayoutParams = moreButton.layoutParams as LayoutParams
params.setMargins(moreButtonParams.leftMargin, padding, moreButtonParams.rightMargin, moreButtonParams.bottomMargin)
params.height = size
moreButton.layoutParams = params
}