android – 如何将GridView集中在其LinearLayout父级中?

前端之家收集整理的这篇文章主要介绍了android – 如何将GridView集中在其LinearLayout父级中?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
GridView的行为并不像它应该的那样.
screenshot显示GridView(在横向模式下)向左刷新.
我希望它居中.

这是GridView的XML布局.

  1. <LinearLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/templatelandscape"
  4. android:orientation="horizontal"
  5. android:layout_width="fill_parent"
  6. android:layout_height="wrap_content">
  7. <GridView
  8. android:id="@+id/commandsbarlandscape"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:layout_weight="1.0"
  12. android:layout_alignParentTop="true"
  13. android:layout_centerInParent="true"
  14. android:padding="0dp"
  15. android:verticalSpacing="2dp"
  16. android:horizontalSpacing="2dp"
  17. android:numColumns="auto_fit"
  18. android:columnWidth="52dp"
  19. android:stretchMode="spacingWidth"
  20. android:gravity="fill_horizontal" />
  21. </LinearLayout>

我究竟做错了什么 ?

解决方法

我不知道你是否解决了你的问题(我希望是的,我也很晚才回答这篇文章!),但这里有一个线索:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/mainLayout"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:gravity="center" >
  7.  
  8. <GridView
  9. android:id="@+id/gridview2"
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"
  12. android:layout_margin="10dp"
  13. android:columnWidth="90dp"
  14. android:numColumns="auto_fit"
  15. android:stretchMode="spacingWidth" >
  16.  
  17. </GridView>
  18. </LinearLayout>

不幸的是,只有在网格上有1个内容时才能工作.也许有人可以改进它!

猜你在找的Android相关文章