我有一个片段,它的布局在 LinearLayout 中包含一个 TextView。我希望在运行时动态更改 textView 的高度,并且我成功地将其更改为比原来更低的值,它似乎有效。但是当我设置一个上限值时,textView 最多只能将其高度调整为父高度。(我检查了父母身高,其值始终为 592)
片段布局.xml
<LinearLayout
android:id="@+id/af_content"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:orientation="vertical" >
<TextView
android:id="@+id/mTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:singleLine="false"
android:text="@string/text"
android:textSize="@dimen/tv_size" />
</LinearLayout>
片段类.java
TextView mTv = (TextView) rootView.findViewById(R.id.mTextView);
int newHeight = value;
mTv.setHeight(value);
如果 value 是像 200 这样的低值,它可以工作,但对于像 1000 这样的更大值,它不会,textview 最多只能调整到父高度。
我也试过这个,但没有任何值的结果,尽管低于或高于原始值。
mTv.measure(MeasureSpec.UNSPECIFIED,MeasureSpec.UNSPECIFIED);
// New measure values
int widthSpec = View.MeasureSpec.makeMeasureSpec(LinearLayout.LayoutParams.WRAP_CONTENT, View.MeasureSpec.UNSPECIFIED);
int heightSpec = View.MeasureSpec.makeMeasureSpec(1200, View.MeasureSpec.EXACTLY);
mTv.measure(widthSpec, heightSpec);
我尝试过使用 invalidate()、requestLayout() 和 refreshDrawableState(),但问题仍然存在。
编辑 1
我已经在 xml 文件中设置 textView 高度值静态,比他的父级长并且它可以工作(尽管我想动态设置它)
android:layout_height="1000dp"
所以 textView 看起来像这样的布局:
片段布局.xml
<LinearLayout
android:id="@+id/af_content"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:orientation="vertical" >
<TextView
android:id="@+id/mTextView"
android:layout_width="wrap_content"
android:layout_height="1000dp"
android:gravity="center"
android:singleLine="false"
android:text="@string/text"
android:textSize="@dimen/tv_size" />
</LinearLayout>
但是现在在运行时我为 textview 设置了滚动方法,它什么也不做,也不滚动:
片段类.java
TextView mTv = (TextView) rootView.findViewById(R.id.mTextView);
mTv.setMovementMethod(new ScrollingMovementMethod());
为什么不滚动?