Unterschied zwischen .getMeasuredWidth() und .getWidth()

  • Antworten:1
Tim Klamm
  • Forum-Beiträge: 2

31.10.2015, 13:54:34 via Website

Hallo,
ich habe in einem Lehrbuch gelesen, dass man mit View.getMeasuredWidth() und View.getWidth() die Breite eines Views abfragen kann. Doch wo genau liegt der Unterschied zwischen diesen beiden Methoden?

Unter welchen Umständen können die Rückgabewerte der beiden Methoden voneinander abweichen?

Vielen Dank für die Antworten

Antworten
Gelöschter Account
  • Forum-Beiträge: 438

01.11.2015, 16:29:08 via Website

Siehe Doku von View:

The size of a view is expressed with a width and a height. A view actually possess two pairs of width and height values.

The first pair is known as measured width and measured height. These dimensions define how big a view wants to be within its parent (see Layout for more details.) The measured dimensions can be obtained by calling getMeasuredWidth() and getMeasuredHeight().

The second pair is simply known as width and height, or sometimes drawing width and drawing height. These dimensions define the actual size of the view on screen, at drawing time and after layout. These values may, but do not have to, be different from the measured width and height. The width and height can be obtained by calling getWidth() and getHeight().

und etwas weiter unten:

Layout is a two pass process: a measure pass and a layout pass. The measuring pass is implemented in measure(int, int) and is a top-down traversal of the view tree. Each view pushes dimension specifications down the tree during the recursion. At the end of the measure pass, every view has stored its measurements. The second pass happens in layout(int, int, int, int) and is also top-down. During this pass each parent is responsible for positioning all of its children using the sizes computed in the measure pass.

Jedes Widget wird also erst "vermessen" und hat dann eine measuredWidht/Height.
Wenn dann im Layout-Pass das Elternwidget andere Maße per layout Methode vorgibt, hat man eine abweichende width/height.

— geändert am 01.11.2015, 16:29:58

Aktuelles Entwicklungsprojekt: (thinking) Sudoku Dojo Free (lightbulb)
Ich freue mich über Tester/innen.

Antworten