Android的getMeasuredHeight返回错误的值!

44

我正在尝试确定一些UI元素的实际像素尺寸!

这些元素是从一个.xml文件中膨胀出来的,并且用dip宽度和高度初始化,以便GUI最终支持多个屏幕尺寸和dpi(按照Android规范建议)。

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="150dip"
android:orientation="vertical">
    
<ImageView
    android:id="@+id/TlFrame" 
    android:layout_width="110dip" 
    android:layout_height="90dip"
    android:src="@drawable/timeline_nodrawing"
    android:layout_margin="0dip"
    android:padding="0dip"/></LinearLayout>

上面的XML表示一个框架。但我会在这里描述的水平布局中动态添加许多框架。

<HorizontalScrollView 
        android:id="@+id/TlScroller"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_margin="0dip"
        android:padding="0dip"
        android:scrollbars="none"
        android:fillViewport="false"
        android:scrollbarFadeDuration="0"
        android:scrollbarDefaultDelayBeforeFade="0"
        android:fadingEdgeLength="0dip"
        android:scaleType="centerInside">
        
        <!-- HorizontalScrollView can only host one direct child -->
        <LinearLayout 
            android:id="@+id/TimelineContent" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_margin="0dip"
            android:padding="0dip"
            android:scaleType="centerInside"/>
   
    </HorizontalScrollView > 

我定义了一个添加一个帧(Frame)的方法在我的Java代码中:

private void addNewFrame()
{       
    LayoutInflater inflater     = (LayoutInflater) _parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ViewGroup root      = (ViewGroup) inflater.inflate(R.layout.tl_frame, null);
    TextView frameNumber = (TextView) root.findViewById(R.id.FrameNumber);
    Integer val = new Integer(_nFramesDisplayed+1); //+1 to display ids starting from one on the user side 
    frameNumber.setText(val.toString());
    
    ++_nFramesDisplayed;
    _content.addView(root);
// _content variable is initialized like this in c_tor
// _content = (LinearLayout) _parent.findViewById(R.id.TimelineContent);
}

然后在我的代码中,我尝试获取实际的像素大小,因为我需要这个来绘制一些OpenGL内容。

LayoutInflater inflater     = (LayoutInflater) _parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ViewGroup root      = (ViewGroup) inflater.inflate(R.layout.tl_frame, null);
    ImageView frame = (ImageView) root.findViewById(R.id.TlFrame);
    
    frame.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    frame.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    final int w = frame.getMeasuredWidth();
    final int h = frame.getMeasuredHeight();

除了这些值比ImageView实际像素大小大得多之外,一切似乎都正常。

从getWindowManager().getDefaultDisplay().getMetrics(metrics)获取的报告信息如下: density = 1.5 densityDpi = 240 widthPixel = 600 heightPixel = 1024

现在,我知道Android的规则是:pixel = dip *(dpi / 160)。但返回的值毫无意义。对于(90dip X 110dip)的ImageView,measure()方法的返回值为(270 x 218),我假设它是以像素为单位的!

有人知道为什么吗? 返回的值是否以像素为单位?

顺便说一句:我已经使用TextView而不是ImageView测试了相同的代码,一切似乎都正常!为什么???

7个回答

60

你使用了错误的调用方式来调用measure方法。

measure方法需要使用MeasureSpec值进行调用,这些值是通过MeasureSpec.makeMeasureSpec方法特殊打包的。该方法会忽略LayoutParams参数。进行测量的父容器需要基于自身的测量和布局策略以及子View的LayoutParams创建一个MeasureSpec。

如果你希望测量出在大多数布局中WRAP_CONTENT的工作原理,可以按照如下方式调用measure

frame.measure(MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.AT_MOST),
        MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST));
如果你没有最大值(例如,如果你正在编写像 ScrollView 这样的具有无限空间的控件),你可以使用 UNSPECIFIED 模式:
frame.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

1
似乎也不起作用...我已经尝试了每种最大值和MesureSpec的组合,结果要么与之前相同,要么是我用于测试的“虚假”最大值或零。 - oberthelot
25
需要澄清的是,如果你想测量自动换行的文本,不能在两个维度上都使用 MeasureSpec.UNSPECIFIED。这会导致水平方向出现无限滚动,并使文本无法自动换行。你需要将垂直维度设置为 MeasureSpec.UNSPECIFIED,水平维度设置为最大宽度,以触发自动换行并获得准确的高度测量值。记得根据需要考虑内边距。 - Bob Aman
第一个方法对我有用。我试图测量在代码中创建的文本视图的尺寸,但字符长度未知,以便我知道在屏幕上相对于其他视图的顶部值的位置。谢谢。 - speedynomads
2
@BobAman 非常感谢您的评论。这完全解决了我对 getMeasuredHeight/Width 的疑问和问题。 - Felipe Lima
这对我也非常有帮助,但不是全部。如果你按照Adamp的建议操作后仍然无法解决问题,那么可以看一下我的解决方案和说明:https://dev59.com/x2kw5IYBdhLWcg3w4emS#36184454 - Hermann Klecker
显示剩余9条评论

19

这样做:

frame.measure(0, 0);

final int w = frame.getMeasuredWidth();
final int h = frame.getMeasuredHeight();

解决了!


@Simon,你的设备是什么? - CoolMind
1
对我来说,这解决了神秘的度量问题,在最初和后续的调用中返回了不同的值,通过指定MeasureSpec.UNSPECIFIED(无意图),感谢! - Pavlus
1
@Pavlus,这与使用 MeasureSpec.UNSPECIFIED 调用相同,因为它是一个常量,其值为0。 - B-GangsteR

5

好的!这里是我的回答……但不完全。

1 - 看起来在一些设备上,ImageView的测量并不能提供准确值。例如,我曾经看到在Nexus和Galaxy设备上发生过这种情况的报告。

2 - 我想出了一个解决方法

在xml代码中将你的ImageView的宽度和高度设置为"wrap_content"。

在你的代码中填充布局(通常在UI初始化时完成)。

LayoutInflater inflater     = (LayoutInflater) 
_parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup root      = (ViewGroup) inflater.inflate(R.layout.tl_frame, null);
ImageView frame = (ImageView) root.findViewById(R.id.TlFrame);

根据典型的Android计算方法,计算您图像视图的比例。

//ScreenDpi can be acquired by getWindowManager().getDefaultDisplay().getMetrics(metrics);
 pixelWidth = wantedDipSize * (ScreenDpi / 160)

使用计算出的大小在代码中动态设置您的ImageView。
frame.getLayoutParams().width = pixeWidth;

完成!现在您的ImageView具有所需的Dip大小;)


4
view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
 @SuppressLint("NewApi")
 @SuppressWarnings("deprecation")
 @Override
  public void onGlobalLayout() {
   //now we can retrieve the width and height
   int width = view.getWidth();
   int height = view.getHeight();


   //this is an important step not to keep receiving callbacks:
   //we should remove this listener
   //I use the function to remove it based on the api level!

    if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN){
        view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    }else{
        view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
    }
  }
 });

如果想获取一个视图(View)的宽度和高度,可以参考如何获取视图的宽度和高度这篇文章。


3

很遗憾,在Activity的生命周期方法中,比如Activity#onCreate(Bundle),布局尚未完成,因此您无法检索视图层次结构中视图的大小。但是,您可以使用View#measure(int, int)显式地请求Android测量视图。

正如@adamp的answer所指出的那样,您必须为View#measure(int, int)提供MeasureSpec值,但是确定正确的MeasureSpec可能有点令人生畏。

下面的方法“尝试”确定正确的MeasureSpec值并测量传入的view:
public class ViewUtil {

    public static void measure(@NonNull final View view) {
        final ViewGroup.LayoutParams layoutParams = view.getLayoutParams();

        final int horizontalMode;
        final int horizontalSize;
        switch (layoutParams.width) {
            case ViewGroup.LayoutParams.MATCH_PARENT:
                horizontalMode = View.MeasureSpec.EXACTLY;
                if (view.getParent() instanceof LinearLayout
                        && ((LinearLayout) view.getParent()).getOrientation() == LinearLayout.VERTICAL) {
                    ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
                    horizontalSize = ((View) view.getParent()).getMeasuredWidth() - lp.leftMargin - lp.rightMargin;
                } else {
                    horizontalSize = ((View) view.getParent()).getMeasuredWidth();
                }
                break;
            case ViewGroup.LayoutParams.WRAP_CONTENT:
                horizontalMode = View.MeasureSpec.UNSPECIFIED;
                horizontalSize = 0;
                break;
            default:
                horizontalMode = View.MeasureSpec.EXACTLY;
                horizontalSize = layoutParams.width;
                break;
        }
        final int horizontalMeasureSpec = View.MeasureSpec
                .makeMeasureSpec(horizontalSize, horizontalMode);

        final int verticalMode;
        final int verticalSize;
        switch (layoutParams.height) {
            case ViewGroup.LayoutParams.MATCH_PARENT:
                verticalMode = View.MeasureSpec.EXACTLY;
                if (view.getParent() instanceof LinearLayout
                        && ((LinearLayout) view.getParent()).getOrientation() == LinearLayout.HORIZONTAL) {
                    ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
                    verticalSize = ((View) view.getParent()).getMeasuredHeight() - lp.topMargin - lp.bottomMargin;
                } else {
                    verticalSize = ((View) view.getParent()).getMeasuredHeight();
                }
                break;
            case ViewGroup.LayoutParams.WRAP_CONTENT:
                verticalMode = View.MeasureSpec.UNSPECIFIED;
                verticalSize = 0;
                break;
            default:
                verticalMode = View.MeasureSpec.EXACTLY;
                verticalSize = layoutParams.height;
                break;
        }
        final int verticalMeasureSpec = View.MeasureSpec.makeMeasureSpec(verticalSize, verticalMode);

        view.measure(horizontalMeasureSpec, verticalMeasureSpec);
    }

}

然后你可以简单地调用:
ViewUtil.measure(view);
int height = view.getMeasuredHeight();
int width = view.getMeasuredWidth();

或者,如@Amit Yadav建议的那样, 您可以使用OnGlobalLayoutListener来在布局完成后调用监听器。以下是一个处理取消注册监听器和版本间方法命名更改的方法:

public class ViewUtil {

    public static void captureGlobalLayout(@NonNull final View view,
                                           @NonNull final ViewTreeObserver.OnGlobalLayoutListener listener) {
        view.getViewTreeObserver()
                .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        final ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                            viewTreeObserver.removeOnGlobalLayoutListener(this);
                        } else {
                            //noinspection deprecation
                            viewTreeObserver.removeGlobalOnLayoutListener(this);
                        }
                        listener.onGlobalLayout();
                    }
                });
    }

}

然后,你可以:

ViewUtil.captureGlobalLayout(rootView, new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        int width = view.getMeasureWidth();
        int height = view.getMeasuredHeight();
    }
});

在您的视图层次结构中,rootView可以是根视图,view可以是您想要知道其尺寸的任何视图。


感谢提供 measure 方法。当使用 new TextView() 创建视图时,它不包含 LayoutParams 但有一个宽度。因此,在 switch (layoutParams.width) 上会出现空指针异常。 - CoolMind
抱歉,这是一种错误的方法,在许多情况下,view.getMeasuredWidth()会返回0。 - CoolMind
使用getViewTreeObserver方法获取尺寸。 - CoolMind
@CoolMind,我提供的“measure”方法考虑了许多常见的布局,但不幸的是并非全部。因此,正如你所提到的,使用“ViewTreeObserver”是更可靠的解决方案。“measure”方法是为了在依赖“ViewTreeObserver”会变得有点混乱的情况下提供的。 - Travis
是的,你说得对,ViewTreeObserver可能不是100%可靠的方法。最好检查isAlive(),参见https://dev59.com/PGcs5IYBdhLWcg3waTXw(但是我可能会出bug)。 - CoolMind

2

您需要创建自定义的TextView并将其用于布局中,使用getActualHeight函数在运行时设置高度。

public class TextViewHeightPlus extends TextView {
    private static final String TAG = "TextViewHeightPlus";
    private int actualHeight=0;


    public int getActualHeight() {
        return actualHeight;
    }

    public TextViewHeightPlus(Context context) {
        super(context);
    }

    public TextViewHeightPlus(Context context, AttributeSet attrs) {
        super(context, attrs);
        setCustomFont(context, attrs);
    }

    public TextViewHeightPlus(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }

   @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        actualHeight=0;

        actualHeight=(int) ((getLineCount()-1)*getTextSize());

    }

}

1
可能是因为你在 AndroidManifest.xml (link) 文件中的设置,以及 xml 文件所在的 drawable-XXX 目录,导致 Android 加载资源时进行了缩放操作。你可以决定使用“dip”(link)作为尺寸单位,它是虚拟的,实际值(px)可能会有所不同。

1
清单文件仅指定活动、意图和 SDK 版本,与屏幕大小或支持无关。此外,我的所有可绘制资源都在基本的 res/drawable 文件夹中,没有 mdpi 或 hdpi 前缀/后缀/子文件夹... - oberthelot
在哪里运行应用程序,是在模拟器上还是在其他设备上? - Damian Kołakowski
ImageView 的比例类型怎么样? - Damian Kołakowski
ScaleType 被设置为 "centerInside" ... 我尝试将其删除,结果仍然相同... - oberthelot
与屏幕大小或支持无关。这不是真的。 - Damian Kołakowski
我指的是 "我的" 清单文件...与屏幕大小或支持无关...不是关于一般的清单文件! - oberthelot

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接