我正在尝试在TextView
NestedScrollView
中显示文字,以便在缩小TextView
自身后显示大于屏幕的文字。
这是我的代码 -
val content = findViewById(R.id.content) as TextView
content.setText(R.string.large_text)
content.viewTreeObserver.addOnGlobalLayoutListener {
Log.d("Height", content.height.toString())//1584:6501
val factor = 1584f / 6501;
content.scaleX = factor;
content.scaleY = factor;
}
我的设备屏幕高度为1584px
且TextView
的内容高度为6501px
问题是NestedScollView
在将TextView
的高度缩小到设备屏幕高度后滚动。
我还尝试在TextView及其父NestedScrollView
上调用invalidate()和requestLayout(),但没有运气。 ScrollView
继续滚动。
此外,我尝试将TextView
和覆盖onMeasure()
方法扩展为
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
val parentWidth = MeasureSpec.getSize(widthMeasureSpec)
val parentHeight = 1584// MeasureSpec.getSize(heightMeasureSpec)
this.setMeasuredDimension(parentWidth, parentHeight)
}
但随后NestedScrollView
停止滚动,TextView
中的内容未显示所有内容。可能TextView
与屏幕的高度相同,因此不会显示所有文本。
如何在缩小后强制'TextView'采用缩放高度,以便NestedScrollView
不滚动?
任何帮助将受到高度赞赏,提前致谢!
修改 这是我的XML代码 -
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.admin.canary.KotlinActivity"
tools:showIn="@layout/activity_kotlin">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v4.widget.NestedScrollView>
</FrameLayout>
答案 0 :(得分:0)
您的观察只是实施的行为。将转换应用于视图实际上不会更改它的任何属性。因此,在您的情况下,视图仅在onDraw()
缩小,而实际大小不会更改。
然后,覆盖onMeasure()
方法将为文本视图本身设置固定大小,而不会将文本拟合为此大小。它只是削减了文本本身。
您还应该考虑到,文本视图已经可滚动,或者至少嵌套滚动视图应该定义android:layout_height="match_parent"
。
有效地安装文本视图并不容易。你可以使用一些库。 AutoFitTextView支持您正在寻找的行为。您必须使用com.vj.widgets.AutoResizeTextView
替换整个布局,并使其与屏幕尺寸相匹配。