【发布时间】:2020-06-23 12:55:39
【问题描述】:
我正在尝试以编程方式将 TextView 放置在其容器的中心,其中仅考虑文本边界,而不是整个 TextView 矩形。 见下图:
还有我的代码:
// "workingLayout" is the container on the TextView
val tvv = TextView(this)
tvv.includeFontPadding = false
tvv.setPadding(20, 0, 20, 0)
tvv.gravity = Gravity.CENTER
tvv.text = "ggg"
tvv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 150f)
tvv.setTextColor(Color.BLUE)
tvv.background = resources.getDrawable(R.drawable.edit_text_shape, null)
tvv.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)
tvv.x = (workingLayout.width / 2f) - (tvv.measuredWidth / 2)
tvv.y = (workingLayout.height / 2f) - (tvv.measuredHeight / 2)
workingLayout.addView(tvv)
我尝试使用 paint.getTextBounds 而不是 measure 但没有运气...
谢谢!
【问题讨论】:
标签: android android-layout textview