在Android中调整布局的动态大小
在具有很多组件的 Activity 中,我有一个包含 WebView 的相对布局(它只显示一个 TextView,我不知道其大小)。
这是xml代码:
<RelativeLayout android:id="@+id/descripcionRelative" android:layout_below="@+id/descripcion_bold" android:layout_width="match_parent" android:layout_height="125dip" android:background="@drawable/my_border">
<WebView android:id="@+id/webViewDescripcion" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</RelativeLayout>
RelativeLayout的属性android:layout_height是125dip,因为如果文本太大,我想定界到125dip。如果文本很大,我会看到带有滚动的文本。伟大的!
但是......如果文字很短,我会看到很多不必要的空间。
一种解决方案是将RelativeLayout的android:layout_height更改为wrap_content。如果文本很短,组件将具有精确的像素,但如果文本太大,我无法对其进行界定。
大问题是我无法计算 WebView 的高度。如果我这样做:descripcion_web.getHeight() 它返回 0。
如果我在这里调用这个方法,它不会返回正确的数字:
descripcion_web.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView webView, String url) {
super.onPageFinished(webView, url);
RelativeLayout marco = (RelativeLayout)findViewById(R.id.descripcionRelative);
System.out.println("Height webView: "+webView.getHeight());
System.out.println("Height relative: "+marco.getHeight());
}
});
我尝试在 onResume() 的方法中调用它,但它不起作用。
解决该问题的另一种尝试是将 android:layout_height 设置为 match_parent 并使用 View 方法 setMaximumHeight(),但它不存在。但是,它确实存在 setMinimumHeight()...
我该如何解决这个问题?非常感谢!
In an Activity with a lot of components, I have a RelativeLayout that contains a WebView (it just shows a TextView, that I don't know the size of).
This is the xml code:
<RelativeLayout android:id="@+id/descripcionRelative" android:layout_below="@+id/descripcion_bold" android:layout_width="match_parent" android:layout_height="125dip" android:background="@drawable/my_border">
<WebView android:id="@+id/webViewDescripcion" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</RelativeLayout>
The attribute android:layout_height of RelativeLayout is 125dip, because if text is too large, I want to delimitate to 125dip. If text is large, I will see text with scroll. Great!
But... if text is short, I see a lot of innecessary space.
One solution is change android:layout_height of RelativeLayout to wrap_content. If text is short, the component will have the exact pixels, but if text is too large, I can't delimitate it.
The BIG PROBLEM is that I can't calculate the WebView's height. If i do: descripcion_web.getHeight() it return 0.
If I call this method here, it doesn't return the right number:
descripcion_web.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView webView, String url) {
super.onPageFinished(webView, url);
RelativeLayout marco = (RelativeLayout)findViewById(R.id.descripcionRelative);
System.out.println("Height webView: "+webView.getHeight());
System.out.println("Height relative: "+marco.getHeight());
}
});
I try to call it in the method in onResume() but it doesn't work.
Another attempt to solve the problem, is set android:layout_height to match_parent and use a View method setMaximumHeight(), but It doesn't exist. However, it does exists setMinimumHeight()...
How can I resolve this? Thank you very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,大多数 Android 视图都没有现成的“setMaximumHeight”。但是你可以实现这样的从WebView继承。以下是如何执行此操作的示例:
希望这会有所帮助!
Unfortunately there is no out of box "setMaximumHeight" for the most Android views. But you can implement such inheriting from the WebView. Here is an example how you can do that:
Hope this helps!