前段时间研究了下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 实现TextView中文字链接的方式
  2. 从Android读取Unity assets下的图片并保存为Bitmap格式
  3. Android的FrameLayout布局介绍
  4. Android自己主动化測试解决方式
  5. android 所有布局属性和UI控件
  6. 转:LinearLayout布局
  7. 开机动画(闪动的ANDROID字样的动画图片)
  8. android自制的软件如何添加到打开方式
  9. android 相对布局 RelativeLayout

随机推荐

  1. Android Studio NDK开发在C代码中将Log输
  2. Android的两种数据存储方式分析(二)
  3. Android轻量型数据库SQLite详解
  4. android的logcat详细用法
  5. Android进程与内存及内存泄露
  6. AsyncTask 很好
  7. android:layout_weight让layout自动调整
  8. Android消息处理机制:源码剖析Handler、Lo
  9. Android华为推送低版本兼容问题
  10. 【Android】EventBus 3.0 源码分析