在19

时间:2015-08-20 16:27:42

标签: java android layout relativelayout layout-inflater

我遇到了问题:

我正在膨胀相对布局,我需要测量其内容的大小。我使用以下代码:

....

    final RelativeLayout relativeLayout = (RelativeLayout) inflater.inflate(id, null, false);

    RelativeLayout relative = (RelativeLayout) relativeLayout.findViewById(R.id.content);

    relativeLayout.measure(0,0);


    widthSoFar = widthSoFar + relative.getMeasuredWidth();


....

使用19之前的API,代码不起作用,Error:NullPointerException in“relativelayout.measure(0,0)”。但是使用API​​ 20或21代码可以很好地运行。

如何使用Api在19之前测量宽度?

谢谢

最好的问候。

1 个答案:

答案 0 :(得分:0)

试试这个:

    final RelativeLayout relativeLayout = (RelativeLayout) inflater.inflate(id, null, false);

    RelativeLayout relative = (RelativeLayout) relativeLayout.findViewById(R.id.content);

    relativeLayout.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

    widthSoFar = widthSoFar + relative.getMeasuredWidth();