gpt4 book ai didi

java - 如何在android中设置带有包装内容的最大高度?

转载 作者:IT老高 更新时间:2023-10-28 20:25:33 24 4
gpt4 key购买 nike

在android中,如何创建一个具有最大高度的 ScrollView 并包裹内容,基本上它垂直包裹内容,但具有最大高度?

我试过了

<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>

但这不起作用?

最佳答案

您可以将其添加到任何 View (在从 View 继承的类中覆盖 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);
}

关于java - 如何在android中设置带有包装内容的最大高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18425758/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com