What does android:layout_weight mean?

前端 未结 13 1237
遇见更好的自我
遇见更好的自我 2020-11-21 06:14

I don\'t understand how to use this attribute. Can anyone tell me more about it?

相关标签:
13条回答
  • 2020-11-21 06:41

    Think it that way, will be simpler

    If you have 3 buttons and their weights are 1,3,1 accordingly, it will work like table in HTML

    Provide 5 portions for that line: 1 portion for button 1, 3 portion for button 2 and 1 portion for button 1

    Regard,

    0 讨论(0)
  • 2020-11-21 06:43

    With layout_weight you can specify a size ratio between multiple views. E.g. you have a MapView and a table which should show some additional information to the map. The map should use 3/4 of the screen and table should use 1/4 of the screen. Then you will set the layout_weight of the map to 3 and the layout_weight of the table to 1.

    To get it work you also have to set the height or width (depending on your orientation) to 0px.

    0 讨论(0)
  • 2020-11-21 06:44

    For additional

    For vertical orientation, don't forget set height to 0dp

    android:layout_height="0dp"
    

    For horizontal orientation, don't forget set width to 0dp

    android:layout_width="0dp"
    
    0 讨论(0)
  • 2020-11-21 06:45

    one of the best explanations for me was this one (from the Android tutorial, look for step 7):

    layout_weight is used in LinearLayouts to assign "importance" to Views within the layout. All Views have a default layout_weight of zero, meaning they take up only as much room on the screen as they need to be displayed. Assigning a value higher than zero will split up the rest of the available space in the parent View, according to the value of each View's layout_weight and its ratio to the overall layout_weight specified in the current layout for this and other View elements.

    To give an example: let's say we have a text label and two text edit elements in a horizontal row. The label has no layout_weight specified, so it takes up the minimum space required to render. If the layout_weight of each of the two text edit elements is set to 1, the remaining width in the parent layout will be split equally between them (because we claim they are equally important). If the first one has a layout_weight of 1 and the second has a layout_weight of 2, then one third of the remaining space will be given to the first, and two thirds to the second (because we claim the second one is more important).

    0 讨论(0)
  • 2020-11-21 06:45

    As the name suggests, Layout weight specifies what amount or percentage of space a particular field or widget should occupy the screen space.
    If we specify weight in horizontal orientation, then we must specify layout_width = 0px.
    Similarly, If we specify weight in vertical orientation, then we must specify layout_height = 0px.

    0 讨论(0)
  • 2020-11-21 06:52

    Adding android:autoSizeTextType="uniform" will resize the text for you automatically

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