在Android中调整布局的动态大小

发布于 2024-12-02 08:04:57 字数 1523 浏览 1 评论 0原文

在具有很多组件的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

む无字情书 2024-12-09 08:04:57

不幸的是,大多数 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);
        }
    }
}

希望这会有所帮助!

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:

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);
        }
    }
}

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文