项目需求:
ScrollView 嵌套 TextView,限制TextView最大高度。
TextView未达到最大高度时,自适应高度且不能滚动
TextView达到最大高度时,可滚动。
解决方案:
public class MaxHeightScrollView extends ScrollView {
private int maxHeight;
public MaxHeightScrollView(Context context) {
this(context, null);
}
public MaxHeightScrollView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MaxHeightScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MaxHeightScrollView);
maxHeight = typedArray.getDimensionPixelSize(R.styleable.MaxHeightScrollView_mhsv_max_height,
(int) ResUtil.getDimension(R.dimen.px_370));
typedArray.recycle();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST));
}
}
使用
<com.general.widget.component.scrollView.MaxHeightScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/app_white"
android:fillViewport="true"
android:overScrollMode="never"
android:paddingStart="@dimen/px_10"
android:paddingTop="@dimen/px_30"
android:paddingEnd="@dimen/px_10"
android:paddingBottom="@dimen/px_40"
android:scrollbars="vertical"
app:mhsv_max_height="@dimen/px_370">
<TextView
android:id="@+id/update_dialog_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/app_space_big"
android:layout_marginEnd="@dimen/app_space_big"
android:includeFontPadding="false"
android:textColor="@color/app_text_summary"
android:textSize="@dimen/pt28" />
</com.general.widget.component.scrollView.MaxHeightScrollView>
网友评论