android:layout_height 50% of the screen size

前端 未结 10 908
无人共我
无人共我 2020-12-23 16:29

I just implemented a ListView inside a LinearLayout, but I need to define the height of the LinearLayout (it has to be 50% of the screen height).



        
10条回答
  •  南方客
    南方客 (楼主)
    2020-12-23 16:49

    Set its layout_height="0dp"*, add a blank View beneath it (or blank ImageView or just a FrameLayout) with a layout_height also equal to 0dp, and set both Views to have a layout_weight="1"

    This will stretch each View equally as it fills the screen. Since both have the same weight, each will take 50% of the screen.

    *See adamp's comment for why that works and other really helpful tidbits.

提交回复
热议问题