歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android九宮格

Android九宮格

日期:2017/3/1 11:11:22   编辑:Linux編程

顯示九宮格需要用GridView , 要顯示每個格子中的視圖有兩種方式,第一種方式是做成xml文件,再將xml文件做成視圖。第二種方式就是在代碼中構建出這樣一種布局,這裡采用第一種方式來實現:

效果:

GridView:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:Android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. >
  6. <!-- id gv_all
  7. 寬高都是填充父窗體
  8. numcolums 為3
  9. 水平控件的距離 10px
  10. 垂直距離是10px
  11. gridview 離底部58px
  12. 離頂部28px
  13. 離左邊5px
  14. 離右邊5px
  15. -->
  16. <GridView
  17. android:id="@+id/gv_all"
  18. android:layout_height="fill_parent"
  19. android:layout_width="fill_parent"
  20. android:numColumns="3"
  21. android:horizontalSpacing="10px"
  22. android:verticalSpacing="10px"
  23. android:layout_marginBottom="58px"
  24. android:layout_marginTop="28px"
  25. android:layout_marginLeft="5px"
  26. android:layout_marginRight="5px"
  27. ></GridView>
  28. </RelativeLayout>
視圖:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="vertical"
  5. android:layout_width="90px"
  6. android:layout_height="90px">
  7. <ImageView
  8. android:layout_width="64px"
  9. android:layout_height="64px"
  10. android:layout_gravity="center_horizontal"
  11. android:id="@+id/main_gv_iv"
  12. />
  13. <TextView
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_gravity="center_horizontal"
  17. android:textSize="16px"
  18. android:textColor="#FFF"
  19. android:id="@+id/main_gv_tv"
  20. />
  21. </LinearLayout>
Copyright © Linux教程網 All Rights Reserved