Jos*_*sue 5 size android webview relativelayout
在具有大量组件的Activity中,我有一个包含WebView的RelativeLayout(它只显示一个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>
Run Code Online (Sandbox Code Playgroud)
RelativeLayout的属性android:layout_height是125dip,因为如果文本太大,我想划分到125dip.如果文字很大,我会看到带滚动的文字.大!
但是......如果文字很短,我会看到很多不必要的空间.
一个解决方案是将RelativeLayout的android:layout_height更改为wrap_content.如果文本很短,则组件将具有精确的像素,但如果文本太大,我无法对其进行分隔.
BIG PROBLEM是我无法计算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());
}
});
Run Code Online (Sandbox Code Playgroud)
我尝试在onResume()中的方法中调用它,但它不起作用.
另一个解决问题的尝试是将android:layout_height设置为match_parent并使用View方法setMaximumHeight(),但它不存在.但是,它确实存在setMinimumHeight()...
我该如何解决这个问题?非常感谢你!
小智 7
不幸的是,大多数Android视图都没有"setMaximumHeight".但是您可以从WebView实现此类继承.以下是如何执行此操作的示例:
package com.am.samples.maxheight;
import android.content.Context;
import android.util.AttributeSet;
import android.webkit.WebView;
public class CustomWebView extends WebView {
private int maxHeightPixels = -1;
public CustomWebView(Context context, AttributeSet attrs, int defStyle,
boolean privateBrowsing) {
super(context, attrs, defStyle, privateBrowsing);
}
public CustomWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomWebView(Context context) {
super(context);
}
public void setMaxHeight(int pixels) {
maxHeightPixels = pixels;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (maxHeightPixels > -1 && getMeasuredHeight() > maxHeightPixels) {
setMeasuredDimension(getMeasuredWidth(), maxHeightPixels);
}
}
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
归档时间: |
|
查看次数: |
2935 次 |
最近记录: |