Android getMeasuredHeight返回错误的值!

obe*_*lot 37 android android-widget android-ui android-layout android-xml

我正在尝试确定一些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>
Run Code Online (Sandbox Code Playgroud)

此前的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 > 
Run Code Online (Sandbox Code Playgroud)

定义为在我的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);
}
Run Code Online (Sandbox Code Playgroud)

然后在我的代码中,我尝试以像素为单位获得实际的实际大小,因为我需要在它上面绘制一些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();
Run Code Online (Sandbox Code Playgroud)

除了那些值大于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,一切似乎都运行正常!为什么!?!?

ada*_*amp 55

你打电话measure不对.

measure获取MeasureSpec特别包装的值MeasureSpec.makeMeasureSpec.measure忽略LayoutParams.进行测量的父级应该根据自己的测量和布局策略以及孩子的LayoutParams创建一个MeasureSpec.

如果要测量WRAP_CONTENT通常在大多数布局中有效的方式,请按以下方式调用measure:

frame.measure(MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.AT_MOST),
        MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST));
Run Code Online (Sandbox Code Playgroud)

如果您没有最大值(例如,如果您正在编写具有无限空间的ScrollView),则可以使用以下UNSPECIFIED模式:

frame.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
Run Code Online (Sandbox Code Playgroud)

  • 为了澄清,如果要测量包装的文本,则不应对两个维使用`MeasureSpec.UNSPECIFIED`.这基本上会给你无限的水平滚动,并导致文本被解开.你需要做的垂直尺寸设置成`MeasureSpec.UNSPECIFIED`和水平尺寸设置为任何的最大宽度,以触发文字环绕,并得到一个准确的高度测量.请记住根据需要考虑填充. (20认同)
  • @BobAman 非常感谢您的评论。这完全解决了我对 getMeasuredHeight/Width 的疑问和问题。 (2认同)

Der*_*rzu 17

去做:

frame.measure(0, 0);

final int w = frame.getMeasuredWidth();
final int h = frame.getMeasuredHeight();
Run Code Online (Sandbox Code Playgroud)

解决了!


obe*_*lot 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);
Run Code Online (Sandbox Code Playgroud)

根据典型的Android计算,计算您自己的图片视图比率

//ScreenDpi can be acquired by getWindowManager().getDefaultDisplay().getMetrics(metrics);
 pixelWidth = wantedDipSize * (ScreenDpi / 160)
Run Code Online (Sandbox Code Playgroud)

使用计算出的大小在代码中动态设置ImageView

frame.getLayoutParams().width = pixeWidth;
Run Code Online (Sandbox Code Playgroud)

瞧!你的ImageView现在有了想要的Dip尺寸;)


Dam*_*ski 1

可能是由于 AndroidManifest.xml (链接)文件中的内容以及 xml 文件来自的 drawable-XXX 目录,Android 通过缩放操作加载资源。您决定使用“dip”(链接)尺寸单位,它是虚拟的,实际值(px)可能不同。