在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>
但这不起作用?
您可以将其添加到任何视图(覆盖从视图继承的类中的 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);
}
您可以通过编程方式完成。
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() 方法。
可以通过将视图包装到 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
与父级的顶部对齐,否则它将居中。
要设置滚动视图高度,您必须同时使用 2 个 linerlayout ,然后将 scrool view 设置为它们的子级,然后设置中间 linerlayout 布局:限制滚动视图高度的高度。
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 的转换对于减轻对它的疑惑是必要的。
对harmashalex答案进行一些修改,我可以为任何视图传递所需的最大高度:
import android.view.View;
import android.view.ViewTreeObserver;
public class MaxHeightForAnyView implements ViewTreeObserver.OnGlobalLayoutListener{
private int maxHeight = 130;
private View view;
public MaxHeightForAnyView(View view, int maxHeight) {
this.view = view;
this.maxHeight = maxHeight ;
}
@Override
public void onGlobalLayout() {
if (view.getHeight() > maxHeight)
view.getLayoutParams().height = maxHeight;
}
}
只需将侦听器设置为 oncreate 中具有所需高度的任何视图即可:
txtShowTrans.getViewTreeObserver().addOnGlobalLayoutListener(new MaxHeightForAnyView(txtShowTrans, 600));
在这里您可以像这样设置 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>