【发布时间】:2014-02-06 08:58:19
【问题描述】:
下面是我的自定义代码View:
XML 布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.project.summary"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/BgColor">
<com.project.summary.customview.CustomView
android:id="@+id/customView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:colorValue="@color/textRed"
app:textString="This the Custom View!!!"
app:textSize="20sp"
/>
</LinearLayout>
代码在我的CustomView.java:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Log.e("140117", "onMeasure()"+this.getHeight()+"||"+this.getWidth());
}
测试活动代码:
public class CustomViewActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.customview_layout);
}
}
logcat 输出:
01-17 13:47:01.203: E/140117(11467): onMeasure()0||0 01-17 13:47:01.243: E/140117(11467): onMeasure()28||212
我搜索了 StackOverflow,但没有人给出明确的答案。有人可以帮我吗?
【问题讨论】:
-
这是正常现象。如果你在
RelativeLayout,你几乎肯定会被测量两次或更多。 -
@kcoppock:谢谢你的回复,你能告诉我为什么它会调用两次吗?触发器是什么?当设置我的自定义视图权重属性时,onMeasure() 调用了四次!为什么?
-
@kcoppock:再次感谢,答案只说:“当您嵌套多个使用权重参数的LinearLayout时,布局传递可能会特别昂贵,这需要对孩子进行两次测量。”,但是不解释什么时候使用权重参数,需要给孩子测量两次?
标签: android android-custom-view onmeasure