ViewPager中NestedScrollView内TextView底部一行被截断问题咨询
解决NestedScrollView内TextView底部内容被截断的问题
我之前也碰到过一模一样的情况,NestedScrollView包裹TextView时,底部一行文字经常会莫名被截断,下面是几个经过验证的有效解决方案:
方案一:给TextView添加底部内边距/外边距
最直接的快速修复方式,给TextView加上底部的padding或者margin,让内容不会被ScrollView的边界“卡”住:
<TextView android:id="@+id/group_footer_pager_item_info" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="8dp" <!-- 也可以用marginBottom,数值建议用dimen资源适配屏幕 --> ... />
方案二:开启NestedScrollView的fillViewport属性
这个是从布局根源解决高度计算偏差的方案,把NestedScrollView的android:fillViewport设为true,它会让ScrollView的子视图填充整个视口高度,避免因高度计算误差导致内容截断:
<android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:fillViewport="true" android:paddingTop="16dp" android:paddingLeft="@dimen/material_horizontal_margin" android:paddingRight="@dimen/material_horizontal_margin" xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 你的TextView布局 --> </android.support.v4.widget.NestedScrollView>
方案三:微调TextView的行间距
如果是字体渲染或行高计算偏差导致的截断,可以给TextView设置android:lineSpacingExtra属性,微调行间距来确保最后一行完整显示:
<TextView android:id="@+id/group_footer_pager_item_info" android:layout_width="match_parent" android:layout_height="wrap_content" android:lineSpacingExtra="1dp" ... />
方案四:代码中强制重新计算TextView高度
如果是动态设置TextView内容后出现的截断,可以在设置文本后强制让TextView重新测量高度,避免ScrollView的布局限制:
TextView textView = findViewById(R.id.group_footer_pager_item_info); textView.setText("你的动态长文本内容"); // 用post确保视图已经完成初始布局后再重新测量 textView.post(() -> { textView.measure( View.MeasureSpec.makeMeasureSpec(textView.getWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED) ); textView.getLayoutParams().height = textView.getMeasuredHeight(); });
内容的提问来源于stack exchange,提问作者Kirill Starostin
相关产品推荐
相关产品推荐

