你好,游客 登录 注册 搜索
背景:
阅读新闻

Android TAb分页菜单实现总结

[日期:2012-08-30] 来源:Linux社区  作者:jj120522 [字体:大 中 小]

第二种方法:GridView+ActivityGroup (图片 ,文字)

(为了省事,我把上下tab分页整理到一个demo里面了.)

这个的布局文件我就不显示了,因为比较简单,我们还是来看代码吧.

代码片段:

  1. /*** 
  2.  * 适配器 
  3.  *  
  4.  * @author Administrator 
  5.  *  
  6.  */  
  7. public class ImageAdapter extends BaseAdapter {  
  8.   
  9.     private Context mContext;  
  10.     private ImageTextButton[] imgItems;  
  11.     private int selResId;  
  12.   
  13.     /*** 
  14.      *  
  15.      * @param c 
  16.      * @param picIds 
  17.      * @param titles 
  18.      * @param width 
  19.      * @param height 
  20.      * @param selResId 
  21.      */  
  22.     public ImageAdapter(Context c, int[] picIds, String titles[], int width,  
  23.             int height, int selResId) {  
  24.         mContext = c;  
  25.         this.selResId = selResId;  
  26.         imgItems = new ImageTextButton[picIds.length];  
  27.         for (int i = 0; i < picIds.length; i++) {  
  28.             imgItems[i] = new ImageTextButton(mContext);  
  29.   
  30.             imgItems[i]  
  31.                     .setLayoutParams(new GridView.LayoutParams(width, height));// 设置ImageView宽高   
  32.             imgItems[i].setPadding(2, 2, 2, 2);  
  33.             // 显示图片与文本   
  34.             imgItems[i].setImageResource(picIds[i], titles[i]);  
  35.         }  
  36.     }  
  37.   
  38.     @Override  
  39.     public int getCount() {  
  40.         return imgItems.length;  
  41.     }  
  42.   
  43.     @Override  
  44.     public Object getItem(int position) {  
  45.         return position;  
  46.     }  
  47.   
  48.     @Override  
  49.     public long getItemId(int position) {  
  50.         return position;  
  51.     }  
  52.   
  53.     /*** 
  54.      * 设置选中后的效果 
  55.      */  
  56.     public void SetFocus(int index) {  
  57.   
  58.         for (int i = 0; i < imgItems.length; i++) {  
  59.             // 先把所有设为最初状态   
  60.             if (i != index) {  
  61.                 imgItems[i].setBackgroundResource(0);// 回到最初样式   
  62.             }  
  63.         }  
  64.         // 选中设置   
  65.         imgItems[index].setBackgroundResource(selResId);  
  66.     }  
  67.   
  68.     @Override  
  69.     public View getView(int position, View convertView, ViewGroup parent) {  
  70.         ImageTextButton imageView;  
  71.         if (convertView == null) {  
  72.             imageView = imgItems[position];  
  73.         } else {  
  74.             imageView = (ImageTextButton) convertView;  
  75.         }  
  76.         return imageView;  
  77.     }  
  78.   
  79. }  

在这里我们用到了自定义控件,其实就是把imageview 和textview 整到一起了,

  1. /*** 
  2.  * 自定义控件(图片文字) 
  3.  */  
  4. public class ImageTextButton extends LinearLayout {  
  5.     private ImageView button = null;  
  6.     private TextView text = null;  
  7.     private Context context;  
  8.   
  9.   
  10.     public ImageTextButton(Context context) {  
  11.         this(context, null);  
  12.         this.context = context;  
  13.     }  
  14.   
  15.   
  16.     public ImageTextButton(Context context, AttributeSet attrs) {  
  17.         super(context, attrs);  
  18.         LayoutInflater.from(context).inflate(R.layout.imagetextbutton, this,  
  19.                 true);  
  20.         button = (ImageView) this.findViewById(R.id.button);  
  21.         text = (TextView) this.findViewById(R.id.btnText);  
  22.         text.setSingleLine(true);  
  23.     }  
  24.   
  25.   
  26.     public void setImageResource(int image_id, String title) {  
  27.         Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),  
  28.                 image_id);  
  29.         button.setBackgroundDrawable(new BitmapDrawable(bitmap));  
  30.         text.setText(title);  
  31.     }  
  32.   
  33.   
  34.     public void setImageBitmap(Bitmap bitmap) {  
  35.         if (button != null)  
  36.             button.setImageBitmap(bitmap);  
  37.     }  
  38.   
  39.   
  40.     public void setBackgroundDrawable(Drawable drawable, int Width, int Hdight) {  
  41.         if (button != null) {  
  42.             button.setBackgroundDrawable(drawable);  
  43.             button.setMinimumHeight(Hdight);  
  44.             button.setMinimumWidth(Width);  
  45.         }  
  46.   
  47.   
  48.     }  
  49.   
  50.   
  51.     public void setText(String title) {  
  52.         if (text != null)  
  53.             text.setText(title);  
  54.     }  
  55.   
  56.   
  57.     public void setText(int ResID) {  
  58.         if (text != null)  
  59.             text.setText(ResID);  
  60.     }  
  61.   
  62.   
  63.     public void setWidth(int width) {  
  64.         button.setMaxWidth(width);  
  65.     }  
  66.   
  67.   
  68.     public void setHeight(int height) {  
  69.         button.setMaxHeight(height);  
  70.     }  
  71.   
  72.   
  73. }  

我们只需要在oncreate中调用即可:

  1. @Override  
  2.     public void onCreate(Bundle savedInstanceState) {  
  3.         super.onCreate(savedInstanceState);  
  4.         setContentView(R.layout.main);  
  5.         Navigation_Top_Bar = (GridView) this  
  6.                 .findViewById(R.id.Navigation_Top_Bar);  
  7.         Navigation_Buttom_Bar = (GridView) this  
  8.                 .findViewById(R.id.Navigation_Buttom_Bar);  
  9.         // 获取显示宽度   
  10.         int width = this.getWindowManager().getDefaultDisplay().getWidth()  
  11.                 / topbar_image_array.length;  
  12.   
  13.         topImgAdapter1 = new ImageAdapter(this, topbar_image_array, titles,  
  14.                 width, 100, R.drawable.cover);  
  15.   
  16.         Init(Navigation_Top_Bar, topImgAdapter1);  
  17.   
  18.         ButtomImgAdapter2 = new ImageAdapter(this, topbar_image_array, titles,  
  19.                 width, 100, R.drawable.cover);  
  20.   
  21.         Init(Navigation_Buttom_Bar, ButtomImgAdapter2);  
  22.   
  23.     }  

这个实现起来有点复杂,不过用习惯了会觉得别有一翻风味的.我之前就一直用这个方法.
在这里我要说明一点:
imgItems[i].setLayoutParams(new GridView.LayoutParams(width, height));// 设置ImageView宽高
其他的都是细节上的问题,我想你们看过都会ok的.
效果图:

        

(怎么样,效果还不错吧。就是实现起来有点负责,不过习惯就好.)

linux
相关资讯       Android开发入门 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款