在android中,如何创建一个具有最大高度的滚动视图,并包装内容,基本上它是垂直包装内容,但是具有最大高度?
我试过
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxHeight="200dp"
android:layout_alignParentBottom="true" >
<LinearLayout
android:id="@+id/maincontainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
但这不起作用?
答案 0 :(得分:26)
您可以将其添加到任何视图(覆盖从视图继承的类中的onMeasure)
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (maxHeight > 0){
int hSize = MeasureSpec.getSize(heightMeasureSpec);
int hMode = MeasureSpec.getMode(heightMeasureSpec);
switch (hMode){
case MeasureSpec.AT_MOST:
heightMeasureSpec = MeasureSpec.makeMeasureSpec(Math.min(hSize, maxHeight), MeasureSpec.AT_MOST);
break;
case MeasureSpec.UNSPECIFIED:
heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
break;
case MeasureSpec.EXACTLY:
heightMeasureSpec = MeasureSpec.makeMeasureSpec(Math.min(hSize, maxHeight), MeasureSpec.EXACTLY);
break;
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
答案 1 :(得分:19)
答案 2 :(得分:15)
您可以通过编程方式完成。
private static class OnViewGlobalLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener {
private final static int maxHeight = 130;
private View view;
public OnViewGlobalLayoutListener(View view) {
this.view = view;
}
@Override
public void onGlobalLayout() {
if (view.getHeight() > maxHeight)
view.getLayoutParams().height = maxHeight;
}
}
将侦听器添加到视图中:
view.getViewTreeObserver()
.addOnGlobalLayoutListener(new OnViewGlobalLayoutListener(view));
当视图高度发生变化时,监听器将调用方法onGlobalLayout()。
答案 3 :(得分:1)
可以通过将视图包装到ConstraintLayout并使用layout_constraintHeight_max
属性来完成。
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="200dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHeight_max="wrap"
app:layout_constraintVertical_bias="0">
...
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
在上面的示例中,父级ConstraintLayout
的高度限制为200dp
,子级ScrollView
的高度将内容包装,直到其小于200dp
。请注意,app:layout_constraintVertical_bias="0"
将孩子ScrollView
对准父对象的顶部,否则它将居中。
答案 4 :(得分:0)
对于设置滚动视图高度,你必须一起使用2个linerlayout,然后设置scrool视图作为子项,然后设置中间衬布布局:高度限制滚动视图高度。
答案 5 :(得分:0)
1。)创建一个类来处理将最大高度设置为用户传递的值:
public class OnViewGlobalLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener {
private Context context;
private int maxHeight;
private View view;
public OnViewGlobalLayoutListener(View view, int maxHeight, Context context) {
this.context = context;
this.view = view;
this.maxHeight = dpToPx(maxHeight);
}
@Override
public void onGlobalLayout() {
if (view.getHeight() > maxHeight) {
ViewGroup.LayoutParams params = view.getLayoutParams();
params.height = maxHeight;
view.setLayoutParams(params);
}
}
public int pxToDp(int px) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int dp = Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
return dp;
}
public int dpToPx(int dp) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
return px;
}
}
2。)将其附加到视图并通过DP中的最大高度:
messageBody.getViewTreeObserver()
.addOnGlobalLayoutListener(
new OnViewGlobalLayoutListener(messageBody, 256, context)
);
感谢@harmashalex的启发。我进行了修改,因为@harma的代码无法设置布局参数。另外,必须进行dp到px的转换才能减轻对它的疑惑。
答案 6 :(得分:-5)
您可以在这里设置Scrollview的高度 - :
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentBottom="true" >
<LinearLayout
android:id="@+id/maincontainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>