Android九宫格


显示九宫格需要用GridView , 要显示每个格子中的视图有两种方式,第一种方式是做成xml文件,再将xml文件做成视图。第二种方式就是在代码中构建出这样一种布局,这里采用第一种方式来实现:

效果:

Android九宫格

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.   
  29. </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>  
  • 1
  • 2
  • 下一页

相关内容