Get view width/height or font width/height/other sizes before first display

后端 未结 2 1945
一向
一向 2020-12-17 05:12

I know that doing view size calculations for placement is frowned up. Android layout managers should place all views properly. However there are some legitimate cases when s

相关标签:
2条回答
  • 2020-12-17 05:37

    The size of view cannot be calculated until its parent is calculated, but you can always force this calculation by calling measure(allowedWidth, allowedHeight) on it. Then getMeasuredWidth() will works.

    see http://developer.android.com/reference/android/view/View.html#measure(int, int)

    0 讨论(0)
  • 2020-12-17 05:41

    Yep I tested the suggestion and it works !

    Sample code.

    Display display = getWindowManager().getDefaultDisplay();
    View s = findViewById(R.id.Screen); // Screen is a container layout
    s.measure(display.getWidth(), display.getHeight());
    View c = findViewById(R.id.Cell0);
    Log.i(TAG, "cell width = " + String.valueOf(c.getMeasuredWidth())
      +  " height = " + String.valueOf(c.getMeasuredHeight()));
    

    What I don't understand is how I missed this ! :) I knew all about the measuring process but I had stuck in its use in custom views ...

    0 讨论(0)
提交回复
热议问题