首先说明的是,我们做APP开发,Tab分页不管是顶部还是底部,都是必不可少的,网上也有太多太多的实现方式了,我在这里总结一下:第一种方式: TabHost原始方式:(链接另一篇文章 http://www.linuxidc.com/Linux/2012-08/69303.htm )这里实现的是底部菜单:布局文件:(我们通过RelativeLayout 可以把TabWidget定位在底部)
- <?xml version="1.0" encoding="utf-8"?>
- <TabHost xmlns:Android="http://schemas.android.com/apk/res/android"
- android:id="@android:id/tabhost"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" >
-
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- android:padding="3dp" >
-
- <FrameLayout
- android:id="@android:id/tabcontent"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1" >
- </FrameLayout>
-
- <TabWidget
- android:id="@android:id/tabs"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_alignBottom="@android:id/tabcontent"
- android:background="@drawable/tabbar_bg" />
- </RelativeLayout>
-
- </TabHost>
在这里我们将说明一下:之前我是获取到TabWidget的view试图及内部icon和title,然后控制实现其效果,但是我们也可以用另外一种方式,也就是我们调用TabHost.TabSpec 的setIndicator(View view);这个方法,我们可以定制显示的view,代码片段:
- /***
- * 创建footerview
- */
- public void createFooterView() {
- tabHost = getTabHost(); // The activity TabHost
-
- view = new TabView(this, R.drawable.tabbar_icon_home,
- R.drawable.tabbar_icon_home_selecotr);
- view.setBackgroundDrawable(this.getResources().getDrawable(
- R.drawable.footer_view_selector));
- intent = new Intent(MainActivity.this, HomeActivity.class);
- spec = tabHost.newTabSpec("num1").setIndicator(view).setContent(intent);
- tabHost.addTab(spec);
-
- view = new TabView(this, R.drawable.tabbar_icon_search,
- R.drawable.tabbar_icon_search_selecotr);
- view.setBackgroundDrawable(this.getResources().getDrawable(
- R.drawable.footer_view_selector));
- intent = new Intent(MainActivity.this, HomeActivity.class);
- spec = tabHost.newTabSpec("num2").setIndicator(view).setContent(intent);
- tabHost.addTab(spec);
-
- view = new TabView(this, R.drawable.tabbar_icon_cart,
- R.drawable.tabbar_icon_cart_selector);
- view.setBackgroundDrawable(this.getResources().getDrawable(
- R.drawable.footer_view_selector));
- intent = new Intent(MainActivity.this, HomeActivity.class);
- spec = tabHost.newTabSpec("num3").setIndicator(view).setContent(intent);
- tabHost.addTab(spec);
-
- view = new TabView(this, R.drawable.tabbar_icon_more,
- R.drawable.tabbar_icon_more_selecotr);
- view.setBackgroundDrawable(this.getResources().getDrawable(
- R.drawable.footer_view_selector));
- intent = new Intent(MainActivity.this, HomeActivity.class);
- spec = tabHost.newTabSpec("num4").setIndicator(view).setContent(intent);
- tabHost.addTab(spec);
- }
- /***
- * 自定义view
- *
- */
- class TabView extends LinearLayout {
- ImageView imageView;
-
- public TabView(Context c, int drawable, int drawableselec) {
- super(c);
- imageView = new ImageView(c);
- // 可以定制点击后状态
- StateListDrawable listDrawable = new StateListDrawable();
- // 未选
- listDrawable.addState(SELECTED_STATE_SET, this.getResources()
- .getDrawable(drawableselec));
- // 选择
- listDrawable.addState(ENABLED_STATE_SET, this.getResources()
- .getDrawable(drawable));
- imageView.setImageDrawable(listDrawable);// 引用 StateListDrawable
- setGravity(Gravity.CENTER);
- addView(imageView);
- }
- }
这样我们就实现想要的效果了.(建议使用这种方法,我的项目就是用的这个实现的.)
如果我是图标和文字分开的,我们也可以用(RadioButton代替,也许大家都不陌生,一会我简单介绍下)
这个源码是因为项目里面用的。