要为NestedScrollView设置最大高度并且显示滚动条,可以尝试以下解决方法:
public class MaxHeightNestedScrollView extends NestedScrollView {
private int maxHeight;
public MaxHeightNestedScrollView(Context context) {
super(context);
}
public MaxHeightNestedScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MaxHeightNestedScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setMaxHeight(int maxHeight) {
this.maxHeight = maxHeight;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int maxHeightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, heightMode);
super.onMeasure(widthMeasureSpec, maxHeightMeasureSpec);
}
}
这样,设置了最大高度的NestedScrollView将显示滚动条,并在内容超过最大高度时进行滚动。
下一篇:Android:未收到广播