How to align LinearLayout at the center of its parent?

前端 未结 14 2037
北荒
北荒 2020-12-04 09:29

I\'ve looked through numerous similar questions here at SO, but nothing helps. I have a hierarchy of different nested layouts, all have android:layout_width=\"fill_par

14条回答
  •  半阙折子戏
    2020-12-04 09:56

    These two attributes are commonly confused:

    • android:gravity sets the gravity of the content of the View it's used on.
    • android:layout_gravity sets the gravity of the View or Layout relative to its parent.

    So either put android:gravity="center" on the parent or android:layout_gravity="center" on the LinearLayout itself.

    I have caught myself a number of times mixing them up and wondering why things weren't centering properly...

提交回复
热议问题