What's the difference between android:height and android:layout_height?

后端 未结 4 576
独厮守ぢ
独厮守ぢ 2021-02-03 19:48



        
相关标签:
4条回答
  • 2021-02-03 20:13

    android:height makes the view be exactly this many pixels tall while android:layout_height specifies the basic height of the view.

    0 讨论(0)
  • 2021-02-03 20:17

    Well, android_layout:height specifies the basic height of the VIEW whereas android:height specifies the height of the object (for example, the button in the xml).

    Good luck! :)

    0 讨论(0)
  • 2021-02-03 20:23

    From playing about with this a bit, I think that layout_height is flexible - a preferred height but will still expand/shrink with the content - and height is a definite value.

    What's happening in your case is that the TextView is wrapping around the Test text but still using the 48dp paddingTop attribute.

    0 讨论(0)
  • 2021-02-03 20:25

    Question 1:

    (what's the difference between them?):

    All attributes starting with »layout_« are hints for the ViewGroup the View is part of. For this each android.view.ViewGroup has a set of LayoutParams. The widget itself does not use them.

    The android:height parameter is the initial size of the android.view.View. But if the View is part ViewGroup then the ViewGroup will resize the objet according to its layout rules.

    BTW: newer APIs have a new android:minHeight and android:maxHeight attribute to clarify programmers intent.

    Note that if you don't set android:height the view will calculate the size itself. Which is right thing to do most of the time.

    Question 2

    (when should one be used over the other?):

    ViewGroup is abstract — the answer to this question depends which actual child class you are using.

    Note that WRAP_CONTENT tells the to use ViewGroup to use android:height. Which means that this is the correct parameter to be used in your example.

    Question 3

    (does the use of one affect the use of the other?):

    Yes — and recursively . TableLayout will read and write the android:width attribute to find out the widest object and resized all other object to it.

    Question 4

    (does the use of one have implications over other attributes which can control the measured dimensions of a view, like padding or margin?)

    No for android:padding but yes for android:layout_margin. Note that margins are a layout parameter and not every layout manager supports margins.

    See margin vs padding for details.

    Final words:

    It is easier to consider all width and height attributes to be hints only. The final say of the size of an object has the layout manager of the used ViewGroup.

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