11

Below is my code for my custom View:

XML layout:

<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>

Code in my CustomView.java:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    Log.e("140117", "onMeasure()"+this.getHeight()+"||"+this.getWidth());
}

Code of the test activity:

    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 output:

01-17 13:47:01.203: E/140117(11467): onMeasure()0||0
01-17 13:47:01.243: E/140117(11467): onMeasure()28||212

I searched StackOverflow, but no one give an clear answer. Could someone help me?

Difster
  • 3,264
  • 2
  • 22
  • 32
linguowu
  • 111
  • 1
  • 6
  • 1
    This is a normal thing. If you're in a `RelativeLayout`, you'll almost certainly be measured twice or more. – Kevin Coppock Jan 17 '14 at 06:39
  • @kcoppock:thanks for your reply,can you tell me why it call twice? what the trigger is ?and when set my customview weight attribute,the onMeasure() called four time! why? – linguowu Jan 17 '14 at 07:01
  • See this answer: http://stackoverflow.com/a/4069077/321697 – Kevin Coppock Jan 17 '14 at 07:02
  • @kcoppock:thanks again,the answer only said that:"The layout pass can be especially expensive when you nest several LinearLayout that use the weight parameter, which requires the child to be measured twice.",but dont give an explanation of when use the weight parameter,it requires the child to be measured twice? – linguowu Jan 17 '14 at 07:13

3 Answers3

12

A parent View may call measure() more than once on its children. For example, the parent may measure each child once with unspecified dimensions to find out how big they want to be, then call measure() on them again with actual numbers if the sum of all the children's unconstrained sizes is too big or too small (that is, if the children don't agree among themselves as to how much space they each get, the parent will intervene and set the rules on the second pass). as https://developer.android.com/guide/topics/ui/how-android-draws.html says.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
fredzzt
  • 133
  • 1
  • 7
0

In the past when I've had troubles similar to this and It's been because my "onAnAction" code should have been inside the activity's class code AKA in the onCreate for the activity instead of in the custom views' class.

This might work but no guarantees, It takes some fiddling and an understanding of the android life cycle usually.

Derek
  • 632
  • 2
  • 11
  • 29
0

Are you missing something in your question? Because if you override onMeasure you must call setMeasuredDimension(int, int) or else you will get an IllegalStateException. In your question you don't seem to get any exceptions.

davidvarghese
  • 637
  • 2
  • 10
  • 18