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

Android TAb分页菜单实现总结

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

第三种方法:ActivityGroup+一些TextView布局.(在这里我们自定实现动态滚动效果)

详情请查看这篇文章:Android 分页Title栏滑块效果--ActionBar(模拟网易 腾讯等动态效果) http://www.linuxidc.com/Linux/2012-08/69305.htm

分页Tab的实现方法和上面方法类是,都是运用ActivityGroup的性质,而上面是通过GridView生成,而我们这边是我们自定义View控件实现.

这里我主要说一下怎样实现ActionBar:

代码片段:

  1. /*** 
  2.  * 自定义控件 
  3.  *  
  4.  * @author zhangjia 
  5.  *  
  6.  *         在这里我要说明一点 我们在创建RectF矩形的时候, 
  7.  *  
  8.  *         参照物原点是所在"父控件的左上角". 
  9.  *  
  10.  */  
  11. public class ActionBar extends LinearLayout implements OnClickListener {  
  12.   
  13.     private ImageView tv1;  
  14.     private ImageView tv2;  
  15.     private ImageView tv3;  
  16.     private ImageView tv4;  
  17.     private Paint paint;// 画笔   
  18.     private RectF curRectF;// draw当前bar   
  19.     private RectF tarRectF;// draw被点击bar   
  20.   
  21.     private final int space_x = 0;// 相当于pading.   
  22.     private final int space_y = 0;// 相当于pading   
  23.     private final double step = 32;// 速度step.   
  24.   
  25.       
  26.     public ActionBar(Context context) {  
  27.         super(context);  
  28.     }  
  29.   
  30.     /*** 
  31.      * 构造方法 
  32.      *  
  33.      * @param context 
  34.      * @param attrs 
  35.      */  
  36.     public ActionBar(Context context, AttributeSet attrs) {  
  37.         super(context, attrs);  
  38.         setWillNotDraw(false);  
  39.         LayoutInflater.from(context).inflate(R.layout.action_bar, this, true);  
  40.         paint = new Paint();  
  41.         paint.setAntiAlias(true);  
  42.         tv1 = (ImageView) findViewById(R.id.tv1);  
  43.         tv2 = (ImageView) findViewById(R.id.tv2);  
  44.         tv3 = (ImageView) findViewById(R.id.tv3);  
  45.         tv4 = (ImageView) findViewById(R.id.tv4);  
  46.         tv1.setOnClickListener(this);  
  47.         tv2.setOnClickListener(this);  
  48.         tv3.setOnClickListener(this);  
  49.         tv4.setOnClickListener(this);  
  50.         curRectF = null;  
  51.         tarRectF = null;  
  52.     }  
  53.   
  54.     /*** 
  55.      * invalidate():调用这个方法会执行onDraw()方法,但是前提是:自己把invalidate()方法执行结束在进行执行. 
  56.      */  
  57.   
  58.     @Override  
  59.     protected void onDraw(Canvas canvas) {  
  60.         super.onDraw(canvas);  
  61.         canvas.drawColor(Color.BLACK);  
  62.         paint.setColor(Color.RED);  
  63.         // 如果当前curRectF=null,也就是第一次访问,则默认为draw第一个bar   
  64.         if (curRectF == null)  
  65.             curRectF = new RectF(tv1.getLeft() + space_x, tv1.getTop()  
  66.                     + space_y, tv1.getRight() - space_x, tv1.getBottom()  
  67.                     - space_y);  
  68.   
  69.         // 第一次方位tarRectF=null,默认为draw   
  70.         if (tarRectF == null)  
  71.             tarRectF = new RectF(tv1.getLeft() + space_x, tv1.getTop()  
  72.                     + space_y, tv1.getRight() - space_x, tv1.getBottom()  
  73.                     - space_y);  
  74.         /*** 
  75.          * 作用:如果在这个范围内则,以这个为最终位置,(不明的白的话,你可以把这个注释运行下你就知道why了.) 
  76.          */  
  77.         if (Math.abs(curRectF.left - tarRectF.left) < step) {  
  78.             curRectF.left = tarRectF.left;  
  79.             curRectF.right = tarRectF.right;  
  80.         }  
  81.   
  82.         /*** 
  83.          * 说明目标在当前的左侧,需要向左移动(每次矩形移动step,则进行invalidate(),从新进行移动...) 
  84.          */  
  85.         if (curRectF.left > tarRectF.left) {  
  86.             curRectF.left -= step;  
  87.             curRectF.right -= step;  
  88.             invalidate();// 继续刷新,从而实现滑动效果,每次step32.   
  89.         }  
  90.         /*** 
  91.          * 说明目标在当前的右侧,需要向右移动(每次矩形移动step,则进行invalidate(),从新进行移动...) 
  92.          */  
  93.         else if (curRectF.left < tarRectF.left) {  
  94.             curRectF.left += step;  
  95.             curRectF.right += step;  
  96.             invalidate();  
  97.         }  
  98.         // canvas.drawRect(curRectF, paint);   
  99.         // 参数,矩形,弧度,画笔   
  100.         canvas.drawRoundRect(curRectF, 5, 5, paint);  
  101.     }  
  102.   
  103.     /**** 
  104.      * 这里要记录目标矩形的坐标 
  105.      */  
  106.     @Override  
  107.     public void onClick(View v) {  
  108.         tarRectF.left = v.getLeft() + space_x;  
  109.         tarRectF.right = v.getRight() - space_x;  
  110.         invalidate();// 刷新   
  111.   
  112.         System.out.println("tarRectF.top=" + tarRectF.top + ",v.getTop()="  
  113.                 + v.getTop() + ", v.getBottom()" + v.getBottom());  
  114.     }  
  115.   
  116. }  

上面已经讲的很详细了,就不啰嗦了.

效果图:

大致就这么多了。

额外:还有一点就是有的会用到RadioButton这个控件,其实就是对其进行了一些调整,这里我简单说明一下应用:

可以取消button样式,用android:drawableTop显示图片,从而达到想要的效果.

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <RadioGroup  
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="wrap_content"  
  9.         android:layout_gravity="bottom"  
  10.         android:background="@drawable/maintab_toolbar_bg"  
  11.         android:gravity="center"  
  12.         android:orientation="horizontal" >  
  13.   
  14.         <RadioButton  
  15.             android:id="@+id/button1"  
  16.             android:layout_width="match_parent"  
  17.             android:layout_height="match_parent"  
  18.             android:layout_gravity="center"  
  19.             android:layout_weight="1"  
  20.             android:background="@drawable/home_btn_bg"  
  21.             android:button="@null"  
  22.             android:drawableTop="@drawable/icon_1_n"  
  23.             android:gravity="center"  
  24.             android:paddingTop="5dp"  
  25.             android:text="首页"  
  26.             android:textSize="12sp" />  
  27.   
  28.         <RadioButton  
  29.             android:id="@+id/button2"  
  30.             android:layout_width="match_parent"  
  31.             android:layout_height="match_parent"  
  32.             android:layout_weight="1"  
  33.             android:background="@drawable/home_btn_bg"  
  34.             android:button="@null"  
  35.             android:drawableTop="@drawable/icon_2_n"  
  36.             android:gravity="center"  
  37.             android:paddingTop="5dp"  
  38.             android:text="短信"  
  39.             android:textSize="12sp" />  
  40.   
  41.         <RadioButton  
  42.             android:id="@+id/button3"  
  43.             android:layout_width="match_parent"  
  44.             android:layout_height="match_parent"  
  45.             android:layout_weight="1"  
  46.             android:background="@drawable/home_btn_bg"  
  47.             android:button="@null"  
  48.             android:drawableTop="@drawable/icon_3_n"  
  49.             android:gravity="center"  
  50.             android:paddingTop="5dp"  
  51.             android:text="联系人"  
  52.             android:textSize="12sp" />  
  53.   
  54.         <RadioButton  
  55.             android:id="@+id/button4"  
  56.             android:layout_width="match_parent"  
  57.             android:layout_height="match_parent"  
  58.             android:layout_weight="1"  
  59.             android:background="@drawable/home_btn_bg"  
  60.             android:button="@null"  
  61.             android:drawableTop="@drawable/icon_4_n"  
  62.             android:gravity="center"  
  63.             android:paddingTop="5dp"  
  64.             android:text="搜索"  
  65.             android:textSize="12sp" />  
  66.     </RadioGroup>  
  67.   
  68. </RelativeLayout>  

这里我们还需要selector.xml

实现点击效果.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.   
  5.     <item android:drawable="@drawable/home_btn_bg_s" android:state_enabled="true" android:state_focused="true" android:state_pressed="false"/>  
  6.     <item android:drawable="@drawable/home_btn_bg_s" android:state_enabled="true" android:state_pressed="true"/>  
  7.     <item android:drawable="@drawable/home_btn_bg_d" android:state_checked="true" android:state_enabled="true"/>  
  8.   
  9.   
  10. </selector>  

示例图:

就说这么多了,情况因人而异.

本篇文章相关代码下载

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是www.linuxidc.com

具体下载目录在 /2012年资料/8月/30日/Android TAb分页菜单实现总结

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

       

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