如何解决无法在Android Studio中的linearLayout中实现-90度旋转的textView
我尝试在我的 Android 应用程序中创建一个 -90 度旋转的薄 TextView(一个水平文本行,例如 30dp 宽度和 400 dp 高度)。不幸的是,TextView 在 LinearLayout 中采用其父级 (LinearLayout) 的高度和宽度,而是显示了一个非常小的正方形(没有文本,因为它太小)。
XML
<LinearLayout
android:layout_width="30dp"
android:layout_height="match_parent"
android:background="#B33C3C3C"
android:id="@+id/verticalLayout"
android:orientation="vertical"
android:gravity="center">
</LinearLayout>
科特林
var width:Int?=null
var height:Int?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity)
var context: Context = this
val vto: ViewTreeObserver = verticalLayout.getViewTreeObserver()
vto.addOnGlobalLayoutListener(object : OnGlobalLayoutListener {
override fun onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
verticalLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this)
} else {
verticalLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this)
}
width = verticalLayout.measuredHeight
height = verticalLayout.measuredWidth
val t = TextView(context)
t.textSize = 15f
t.text = "Testttttttttttttttttttt"
t.rotation = -90f
t.setBackgroundResource(R.color.primary)
t.height = convertPixelsToDp(convertPixelsToDp(height!!,context),context)
t.width = convertPixelsToDp(convertPixelsToDp(width!!,context)
Log.d("aaa","height " + height + " width " + width)
Log.d("aaa","height " + convertPixelsToDp(height!!,context) + " width " + convertPixelsToDp(width!!,context))
verticalLayout.addView(t)
}
})
}
fun convertPixelsToDp(px: Int,context: Context): Int {
return px / (context.resources
.displayMetrics.densityDpi.toInt() / displayMetrics.DENSITY_DEFAULT)
}
Log.d 实际上显示了正确的数字:
height 90 width 1415
height 30 width 471.
您对如何实现所需的细长旋转文本有任何想法吗?谢谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。