What is the trick with 0dip layout_height or layouth_width?

后端 未结 3 1802
孤城傲影
孤城傲影 2020-12-05 15:00

I mean why anybody want they view to be 0dip height ? I have seen this many times, there must be some kind of trick, but I do not get it.

        

        
3条回答
  •  半阙折子戏
    2020-12-05 15:34

    This is heavily used for views withing LinearLayout. There are three "layout" attributes that LinearLayout is aware of:

    1. android:layout_height
    2. android:layout_width
    3. android:layout_weight

    You can find example with android:layout_weight in tutorial project.

    So when android:layout_weight is used on View X and LinearLayout is horizontal, then X's android:layout_width is simply ignored.

    Similar, when android:layout_weight is used on View X and LinearLayout is vertical, then X's android:layout_height is ignored.

    This actually means, that you can put anything in those ignored fields: 0dp or fill_parent or wrap_content. It doesn't matter. But it's recommended to use 0dp so View's do not do extra calculation of their height or width (which is then ignored). This small trick simply saves CPU cycles.

提交回复
热议问题