所以我有一个textview,其大小是根据android的autoSizeTextType =“uniform”设置的。我想将该大小复制到其他两个文本 View 中。
这是我尝试过的:
val firstButton = activity?.findViewById<TextView>(R.id.b1)
val secondButton = activity?.findViewById<TextView>(R.id.b2)
val thirdButton = activity?.findViewById<TextView>(R.id.b3)
if (firstButton != null) {
secondButton?.textSize = firstButton.textSize
thirdButton?.textSize = firstButton.textSize
}
但是,这导致2和3的文本大小大约是第一个的4倍。有人对我做错了什么吗?我猜它正在获得某种dpi缩放尺寸,但是我不确定如何获得正确的尺寸。
最佳答案
TextView.getTextSize()
返回size in pixels,但是setTextSize()
将参数解释为缩放的像素(sp)。有setTextSize() overload接受文本大小和单位。在您的情况下,单位应为TypedValue.COMPLEX_UNIT_PX
。
secondButton?.setTextSize(TypedValue.COMPLEX_UNIT_PX, firstButton.textSize)
关于android - Android将一个TextView的文本大小设置为等于另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61666242/