缩小子视图的高度相同

我试图在NestedScrollView中的TextView中显示文本,使得文本比屏幕更大,在缩小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; } 

我的设备屏幕高度是1584pxTextView的内容高度是6501px

问题是NestedScollView sill在缩小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> 

你的观察只是实施的行为。 将转换应用到视图并不会改变它的任何属性。 所以在你的情况下,视图只在onDraw()缩小,而实际大小不变。

重写onMeasure()方法将为文本视图本身设置一个固定的大小,而不需要将文本调整为这个大小。 它只是削减了文本本身。

你还应该考虑到,文本视图已经可以滚动,或者至少嵌套的滚动视图应该定义android:layout_height="match_parent"


有效地装配文本视图不是太容易。 你可以使用一些库。 AutoFitTextView支持你正在寻找的行为。 您必须用com.vj.widgets.AutoResizeTextView替换完整的布局,并让它与屏幕大小相匹配。