ome*_*ega 29 java xml height android
在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>
Run Code Online (Sandbox Code Playgroud)
但这不起作用?
bab*_*bay 29
你可以将它添加到任何视图(覆盖从视图继承的类中的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);
}
Run Code Online (Sandbox Code Playgroud)
JMP*_*gar 19
我扩展了ScrollView并添加了代码来实现此功能:
https://gist.github.com/JMPergar/439aaa3249fa184c7c0c
我希望这很有用.
小智 18
你可以通过编程方式完成.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
并向视图添加侦听器:
view.getViewTreeObserver()
.addOnGlobalLayoutListener(new OnViewGlobalLayoutListener(view));
Run Code Online (Sandbox Code Playgroud)
当视图高度发生变化时,监听器将调用方法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>
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,父级ConstraintLayout高度限制为200dp,子级ScrollView高度将内容包裹起来,直到小于200dp。请注意,app:layout_constraintVertical_bias="0"将子项ScrollView与父项的顶部对齐,否则它将居中。
Abh*_*ade -7
在这里您可以像这样设置 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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
42324 次 |
| 最近记录: |