Java 如何在android中设置包装内容的最大高度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18425758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set a maximum height with wrap content in android?
提问by omega
In android, how can you create a scroll view that's got a max height, and wrap contents, basically it wraps the content vertically, but has a maximum height?
在android中,如何创建一个具有最大高度的滚动视图并包裹内容,基本上它垂直包裹内容,但具有最大高度?
I tried
我试过
<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>
But this isn't working?
但这行不通?
回答by Abhishek Birdawade
Here you can set height of your Scrollview like this -:
在这里,您可以像这样设置 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>
回答by JMPergar
I've extended ScrollView and added code to implement this feature:
我扩展了 ScrollView 并添加了代码来实现此功能:
https://gist.github.com/JMPergar/439aaa3249fa184c7c0c
https://gist.github.com/JMPergar/439aaa3249fa184c7c0c
I hope that be useful.
我希望这是有用的。
回答by harmashalex
You can do it programmatically.
您可以以编程方式进行。
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;
}
}
And add listener to the view:
并向视图添加侦听器:
view.getViewTreeObserver()
.addOnGlobalLayoutListener(new OnViewGlobalLayoutListener(view));
Listener will call method onGlobalLayout(), when view height will be changed.
当视图高度发生变化时,侦听器将调用方法 onGlobalLayout()。
回答by babay
you can add this to any view (override onMeasure in a class inherited from a 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);
}
回答by Rasool_sof
for set scrollview height you must use 2 linerlayout inside together and then set scrool view as them child then set middle linerlayout layout:height for limit scrollview height.
要设置滚动视图高度,您必须将 2 个 linerlayout 一起使用,然后将滚动视图设置为它们的孩子,然后设置中间 linerlayout layout:height 以限制滚动视图高度。
回答by Yash Krishan
1.) Create a class to handle setting maximum height to what is passed by the user:
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.) Attach this to the view and pass the maximum height in DP:
2.) 将此附加到视图并通过 DP 中的最大高度:
messageBody.getViewTreeObserver()
.addOnGlobalLayoutListener(
new OnViewGlobalLayoutListener(messageBody, 256, context)
);
Thanks to @harmashalex for the inspiration. I made modifications to as setting the layout params didn't work by @harma's code. Also, dp-to-px conversion is necessary to offload wondering about it.
感谢@harmashalex 的启发。我进行了修改,因为@harma 的代码设置布局参数不起作用。此外,dp 到 px 的转换对于减轻对它的疑惑是必要的。