以下是我自定义 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
中,几乎肯定会被测量两次或更多次。 - Kevin Coppock