前段时间研究了下android中九宫格布局的实现。纵观现在的应用程序,九宫格是非常常见的一种布局方式。很多优秀的手机应用程序都采用了这一布局。下面就android中九宫格布局方式的实现和大家做一个简单的介绍。

首先是main.xml的布局方式如下:

        
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <!--主界面的布局-->
  3. <RelativeLayout
  4. xmlns:android="http://schemas.android.com/apk/res/android"
  5. android:orientation="vertical"
  6. android:layout_width="fill_parent"
  7. android:layout_height="fill_parent"
  8. >
  9. <RelativeLayout
  10. android:id="@+id/MainActivityrlTwo"
  11. android:layout_width="fill_parent"
  12. android:layout_height="45dp"
  13. >
  14. </RelativeLayout>
  15. <GridView
  16. android:id="@+id/MainActivityGrid"
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content"
  19. android:numColumns="3"
  20. android:columnWidth="50dp"
  21. android:layout_below="@+id/MainActivityrlTwo"
  22. android:layout_marginTop="5dp"
  23. />
  24. <RelativeLayout
  25. android:id="@+id/MainActivityrlThree"
  26. android:layout_width="fill_parent"
  27. android:layout_height="60dp"
  28. android:layout_alignParentBottom="true"
  29. >
  30. <TextView
  31. android:id="@+id/tvLineBottom"
  32. android:layout_width="fill_parent"
  33. android:layout_height="wrap_content"
  34. android:text="@string/line_default"
  35. />
  36. <Button
  37. android:id="@+id/btmore_MainActivity"
  38. android:layout_alignParentRight="true"
  39. android:layout_alignParentBottom="true"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:text="More"
  43. />
  44. </RelativeLayout>
  45. </RelativeLayout>

--------------------------------------------------------------------------------------------------

其次就是每一格九宫格的布局方式:

        
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <!--九宫格每一格的布局-->
  3. <LinearLayout
  4. xmlns:android="http://schemas.android.com/apk/res/android"
  5. android:orientation="vertical"
  6. android:layout_width="fill_parent"
  7. android:layout_height="fill_parent"
  8. >
  9. <ImageView
  10. android:id="@+id/MainActivityImage"
  11. android:layout_width="50dp"
  12. android:layout_height="50dp"
  13. android:layout_gravity="center_horizontal"
  14. />
  15. <TextView
  16. android:id="@+id/MainActivityText"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:layout_gravity="center_horizontal"
  20. android:textSize="18sp"
  21. android:lines="1"
  22. android:layout_marginTop="8dp"
  23. />
  24. </LinearLayout>

--------------------------------------------------------------------------------------------------

最后就是adapter的编写:

        
  1. publicclassImageAdapterextendsBaseAdapter{
  2. privateContextcontext;
  3. publicImageAdapter(Contextcontext){
  4. this.context=context;
  5. }
  6. privateInteger[]images={
  7. //九宫格图片的设置
  8. R.drawable.icon_1,
  9. R.drawable.icon_2,
  10. R.drawable.icon_3,
  11. R.drawable.icon_4,
  12. R.drawable.icon_5,
  13. R.drawable.icon_6,
  14. R.drawable.icon_7,
  15. R.drawable.icon_8,
  16. R.drawable.icon_9,
  17. };
  18. privateString[]texts={
  19. //九宫格图片下方文字的设置
  20. "记录支出",
  21. "记录收入",
  22. "账本管理",
  23. "类别管理",
  24. "查看图表",
  25. "收支对照",
  26. "记录心得",
  27. "新闻公告",
  28. "系统设置",
  29. };
  30. //getthenumber
  31. @Override
  32. publicintgetCount(){
  33. returnimages.length;
  34. }
  35. @Override
  36. publicObjectgetItem(intposition){
  37. returnposition;
  38. }
  39. //getthecurrentselector'sidnumber
  40. @Override
  41. publiclonggetItemId(intposition){
  42. returnposition;
  43. }
  44. //createviewmethod
  45. @Override
  46. publicViewgetView(intposition,Viewview,ViewGroupviewgroup){
  47. ImgTextWrapperwrapper;
  48. if(view==null){
  49. wrapper=newImgTextWrapper();
  50. LayoutInflaterinflater=LayoutInflater.from(context);
  51. view=inflater.inflate(R.layout.item,null);
  52. view.setTag(wrapper);
  53. view.setPadding(15,15,15,15);//每格的间距
  54. }else{
  55. wrapper=(ImgTextWrapper)view.getTag();
  56. }
  57. wrapper.imageView=(ImageView)view.findViewById(R.id.MainActivityImage);
  58. wrapper.imageView.setBackgroundResource(images[position]);
  59. wrapper.textView=(TextView)view.findViewById(R.id.MainActivityText);
  60. wrapper.textView.setText(texts[position]);
  61. returnview;
  62. }
  63. }
  64. classImgTextWrapper{
  65. ImageViewimageView;
  66. TextViewtextView;
  67. }

--------------------------------------------------------------------------------------------------

当然最后的最后就是你得自己准备九张漂亮的图片啦!

九宫格的主界面大功告成!如果还有什么疑问可以留言哈…欢迎交流

本人QQ:523072842

更多相关文章

  1. 开机动画(闪动的ANDROID字样的动画图片)
  2. Android的FrameLayout布局介绍
  3. 从Android读取Unity assets下的图片并保存为Bitmap格式
  4. Android自己主动化測试解决方式
  5. Android中的SQLite使用学习
  6. 开机动画(闪动的ANDROID字样的动画图片)
  7. android自制的软件如何添加到打开方式
  8. Android的四种启动方式
  9. Android开机动画过程

随机推荐

  1. Android(安卓)UI开发第二十五篇——分享
  2. android Lru图片缓存管理方案
  3. Android上鲜为人知的UI控件介绍和使用
  4. Android(安卓)context 文件模式
  5. Android的性能优化方法
  6. Android(安卓)进程保活,Service进程常驻
  7. Android数据库操作--greenDAO的入门使用
  8. Android(安卓)如何在关于手机界面添加个
  9. 不需要任何权限获得Android设备的唯一ID
  10. Android教学大纲(知识体系结构)