Android: center gridview horizontally

前端 未结 4 788
小蘑菇
小蘑菇 2020-12-06 06:48

I need to center grid view horizontally in my android layout.xml. I have searched google for quite a while but didnt succeed in finding an answer.

I can only change

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-06 07:21

    Set the stretch mode to columnWidth on your GridView layout or style:

    android:stretchMode="columnWidth"

    The column width will stretch to fill the available space. You can wrap your grid items in a transparent container that will stretch evenly while preserving the width of your visible content. The android:columnWidth attribute will function as "minimum column width." This also works with android:numColumns=auto_fit, android:horizontalSpacing and android:verticalSpacing.

    Depending on what your grid items are you may not want to add a transparent container around your content. If you don't, the content width will stretch evenly which may actually look better on more devices.

提交回复
热议问题