Mike..
19
在这个答案的帮助下,我设法结束了一个非常基本的ScrollView组件,你可以用于这种情况:
创建一个扩展ScrollView的自定义类并执行以下修改:
public class MaxHeightScrollView extends ScrollView {
private int maxHeight;
public MaxHeightScrollView(Context context) {
super(context);
}
public MaxHeightScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public MaxHeightScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public MaxHeightScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
if (attrs != null) {
TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.MaxHeightScrollView);
maxHeight = styledAttrs.getDimensionPixelSize(R.styleable.MaxHeightScrollView_maxHeight, 200); //200 is a default value
styledAttrs.recycle();
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
然后剩下的一件小事就是在您的values文件夹的attrs.xml文件中声明您的样式(如果您没有,请在项目的res文件夹的values文件夹中创建一个带有此名称的xml文件).在那里添加以下行:
并使用您的新ScrollView如下:
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:maxHeight="300dp">
积分去whizzle快速包装这件事!