1、继承TabActivity

package com.example.yqqmobilesafe;import android.app.TabActivity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.GridView;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TabHost;import android.widget.TextView;public class HomeActivity extends TabActivity { private TabHost mTabHost;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_home);        initViews();    }    private void initViews() {mTabHost=getTabHost();//获取到最外层的容器.Intent intent1=new Intent(this,OftenUseFunctionActivity.class);mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("常用功能",getResources().getDrawable(R.drawable.accelerate_selector)).setContent(intent1));Intent intent2=new Intent(this,AppManagerActivity.class);mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("软件管理",getResources().getDrawable(R.drawable.soft_manager_selector)).setContent(intent2));Intent intent3=new Intent(this,SafeProtectActivity.class);mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("安全防护",getResources().getDrawable(R.drawable.security_selector)).setContent(intent3));Intent intent4=new Intent(this,PrivateProtectActivity.class);mTabHost.addTab(mTabHost.newTabSpec("tab4").setIndicator("隐私保护",getResources().getDrawable(R.drawable.profile_selector)).setContent(intent4));}  @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.home, menu);        return true;    }    }

布局文件:
activity_home.xml
<pre name="code" class="html"><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"    android:background="#ffffff"     >    <RelativeLayout        android:layout_width="fill_parent"        android:layout_height="fill_parent" >        <TabWidget            android:layout_alignParentBottom="true"            android:id="@android:id/tabs"            android:layout_width="fill_parent"            android:background="#11ff52"            android:layout_height="wrap_content" >        </TabWidget>        <FrameLayout            android:layout_marginBottom="60dip"            android:id="@android:id/tabcontent"            android:layout_width="fill_parent"            android:layout_height="fill_parent" >        </FrameLayout>    </RelativeLayout></TabHost>

效果如下:

  
<img src="http://img.blog.csdn.net/20141118190520652?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxNDYwMDQzMg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /><img src="http://img.blog.csdn.net/20141118190530218?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxNDYwMDQzMg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
  

2、没有继承TabActivity

/** * 初始化UI组件 */private void initViews() {setContentView(R.layout.activity_home);mTabHost=(TabHost)this.findViewById(R.id.tabhost);mTabHost.setup();mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("第一个",getResources().getDrawable(R.drawable.unresloved_selector)).setContent(R.id.lv_leave_infos));mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("第二个", getResources().getDrawable(R.drawable.leave_apply_selector)).setContent(R.id.ll_tb_leave_apply));mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("第三个", getResources().getDrawable(R.drawable.exception_note_selector)).setContent(R.id.ll_tb_leave_excepiton_note));mTabHost.setCurrentTabByTag("tab1");mleaveInfos=new ArrayList<LeaveInfo>();mLeaveInfosDao=new LeaveInfosDao(HomeActivity.this);mAcceptLeaveInfoDao=new AcceptLeaveInfosDao(HomeActivity.this);mRejectLeaveInfoDao=new RejectLeaveInfosDao(HomeActivity.this);//mLLloading=(View)this.findViewById(R.id.ll_loading);mLVleaveInfos=(ListView)this.findViewById(R.id.lv_leave_infos);fillData();}

该acticity对应的布局文件:
<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/tabhost"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_marginBottom="1dp"    android:background="#ffffff"    android:orientation="vertical" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical" >        <FrameLayout            android:id="@android:id/tabcontent"            android:layout_width="match_parent"            android:layout_height="wrap_content" >            <!-- 第一个 -->            <LinearLayout                android:id="@+id/ll_tb_unresolve"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:orientation="vertical" >                <RelativeLayout                    android:layout_width="match_parent"                    android:layout_height="50dp"                    android:background="@drawable/tabitem_bgpress" >                    <TextView                        android:id="@+id/tv_app_sys"                        android:layout_width="wrap_content"                        android:layout_height="45dp"                        android:layout_marginLeft="5dp"                        android:gravity="center_vertical"                        android:text="@string/ehr_manager_system"                        android:textColor="#ffffff" />                    <ImageView                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_alignParentRight="true"                        android:layout_alignParentTop="true"                        android:clickable="true"                        android:onClick="addInfo"                        android:src="@drawable/add_selector" />                </RelativeLayout>                                   <ListView                        android:id="@+id/lv_leave_infos"                        android:layout_width="match_parent"                        android:layout_height="wrap_content" />                          </LinearLayout>            <!-- 第2个 -->            <LinearLayout                android:layout_marginTop="45dp"                android:id="@+id/ll_tb_leave_apply"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:orientation="vertical" >               <ListView                    android:id="@+id/lv_leave_info_accept"                    android:layout_width="match_parent"                    android:layout_height="wrap_content" />            </LinearLayout>            <!-- 第3个 -->            <LinearLayout                android:layout_marginTop="45dp"                android:id="@+id/ll_tb_leave_excepiton_note"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:orientation="vertical" >                <!-- 加载数据时候显示 -->                <ListView                    android:id="@+id/lv_leave_info_reject"                    android:layout_width="match_parent"                    android:layout_height="wrap_content" />            </LinearLayout>        </FrameLayout>    </LinearLayout>    <TabWidget        android:id="@android:id/tabs"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_gravity="bottom" /></TabHost>

给导航设置监听:
/** * 设置监听 */private void setListener(){mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {@Overridepublic void onTabChanged(String tabId) {if(tabId.equals("tab1")){//mLVleaveInfos=(ListView)findViewById(R.id.lv_leave_infos);fillData();Log.i("导航","第一个");}else if(tabId.equals("tab2")){mLVleaveInfosAccept=(ListView)findViewById(R.id.lv_leave_info_accept);fillDataAccept();showDeleteOrNo();//是否删除记录Log.i("导航","第二个");}else if(tabId.equals("tab3")){mLVleaveInfoReject=(ListView)findViewById(R.id.lv_leave_info_reject);fillDataReject();showDeleteOrNoReject();//是否删除记录Log.i("导航","第三个");}}});


3、ViewPager实现导航效果

public class MainWeixin extends Activity {public static MainWeixin instance = null; private ViewPager mTabPager;private ImageView mTabImg;// 动画图片private ImageView mTab1,mTab2,mTab3,mTab4;private int zero = 0;// 动画图片偏移量private int currIndex = 0;// 当前页卡编号private int one;//单个水平动画位移private int two;private int three;private LinearLayout mClose;    private LinearLayout mCloseBtn;    private View layout;private boolean menu_display = false;private PopupWindow menuWindow;private LayoutInflater inflater;//private Button mRightBtn;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main_weixin);         //启动activity时不自动弹出软键盘        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);         instance = this;        /*        mRightBtn = (Button) findViewById(R.id.right_btn);        mRightBtn.setOnClickListener(new Button.OnClickListener(){@Overridepublic void onClick(View v){showPopupWindow (MainWeixin.this,mRightBtn);}  });*/                mTabPager = (ViewPager)findViewById(R.id.tabpager);        mTabPager.setOnPageChangeListener(new MyOnPageChangeListener());                mTab1 = (ImageView) findViewById(R.id.img_weixin);        mTab2 = (ImageView) findViewById(R.id.img_address);        mTab3 = (ImageView) findViewById(R.id.img_friends);        mTab4 = (ImageView) findViewById(R.id.img_settings);        mTabImg = (ImageView) findViewById(R.id.img_tab_now);        mTab1.setOnClickListener(new MyOnClickListener(0));        mTab2.setOnClickListener(new MyOnClickListener(1));        mTab3.setOnClickListener(new MyOnClickListener(2));        mTab4.setOnClickListener(new MyOnClickListener(3));        Display currDisplay = getWindowManager().getDefaultDisplay();//获取屏幕当前分辨率        int displayWidth = currDisplay.getWidth();        int displayHeight = currDisplay.getHeight();        one = displayWidth/4; //设置水平动画平移大小        two = one*2;        three = one*3;        //Log.i("info", "获取的屏幕分辨率为" + one + two + three + "X" + displayHeight);                //InitImageView();//使用动画      //将要分页显示的View装入数组中        LayoutInflater mLi = LayoutInflater.from(this);        View view1 = mLi.inflate(R.layout.main_tab_weixin, null);        View view2 = mLi.inflate(R.layout.main_tab_address, null);        View view3 = mLi.inflate(R.layout.main_tab_friends, null);        View view4 = mLi.inflate(R.layout.main_tab_settings, null);              //每个页面的view数据        final ArrayList<View> views = new ArrayList<View>();        views.add(view1);        views.add(view2);        views.add(view3);        views.add(view4);      //填充ViewPager的数据适配器        PagerAdapter mPagerAdapter = new PagerAdapter() {@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {return arg0 == arg1;}@Overridepublic int getCount() {return views.size();}@Overridepublic void destroyItem(View container, int position, Object object) {((ViewPager)container).removeView(views.get(position));}//@Override//public CharSequence getPageTitle(int position) {//return titles.get(position);//}@Overridepublic Object instantiateItem(View container, int position) {((ViewPager)container).addView(views.get(position));return views.get(position);}};mTabPager.setAdapter(mPagerAdapter);    }    /** * 头标点击监听 */public class MyOnClickListener implements View.OnClickListener {private int index = 0;public MyOnClickListener(int i) {index = i;}@Overridepublic void onClick(View v) {mTabPager.setCurrentItem(index);}};     /* 页卡切换监听(原作者:D.Winter) */public class MyOnPageChangeListener implements OnPageChangeListener {@Overridepublic void onPageSelected(int arg0) {Animation animation = null;switch (arg0) {case 0:mTab1.setImageDrawable(getResources().getDrawable(R.drawable.tab_weixin_pressed));if (currIndex == 1) {animation = new TranslateAnimation(one, 0, 0, 0);mTab2.setImageDrawable(getResources().getDrawable(R.drawable.tab_address_normal));} else if (currIndex == 2) {animation = new TranslateAnimation(two, 0, 0, 0);mTab3.setImageDrawable(getResources().getDrawable(R.drawable.tab_find_frd_normal));}else if (currIndex == 3) {animation = new TranslateAnimation(three, 0, 0, 0);mTab4.setImageDrawable(getResources().getDrawable(R.drawable.tab_settings_normal));}break;case 1:mTab2.setImageDrawable(getResources().getDrawable(R.drawable.tab_address_pressed));if (currIndex == 0) {animation = new TranslateAnimation(zero, one, 0, 0);mTab1.setImageDrawable(getResources().getDrawable(R.drawable.tab_weixin_normal));} else if (currIndex == 2) {animation = new TranslateAnimation(two, one, 0, 0);mTab3.setImageDrawable(getResources().getDrawable(R.drawable.tab_find_frd_normal));}else if (currIndex == 3) {animation = new TranslateAnimation(three, one, 0, 0);mTab4.setImageDrawable(getResources().getDrawable(R.drawable.tab_settings_normal));}break;case 2:mTab3.setImageDrawable(getResources().getDrawable(R.drawable.tab_find_frd_pressed));if (currIndex == 0) {animation = new TranslateAnimation(zero, two, 0, 0);mTab1.setImageDrawable(getResources().getDrawable(R.drawable.tab_weixin_normal));} else if (currIndex == 1) {animation = new TranslateAnimation(one, two, 0, 0);mTab2.setImageDrawable(getResources().getDrawable(R.drawable.tab_address_normal));}else if (currIndex == 3) {animation = new TranslateAnimation(three, two, 0, 0);mTab4.setImageDrawable(getResources().getDrawable(R.drawable.tab_settings_normal));}break;case 3:mTab4.setImageDrawable(getResources().getDrawable(R.drawable.tab_settings_pressed));if (currIndex == 0) {animation = new TranslateAnimation(zero, three, 0, 0);mTab1.setImageDrawable(getResources().getDrawable(R.drawable.tab_weixin_normal));} else if (currIndex == 1) {animation = new TranslateAnimation(one, three, 0, 0);mTab2.setImageDrawable(getResources().getDrawable(R.drawable.tab_address_normal));}else if (currIndex == 2) {animation = new TranslateAnimation(two, three, 0, 0);mTab3.setImageDrawable(getResources().getDrawable(R.drawable.tab_find_frd_normal));}break;}currIndex = arg0;animation.setFillAfter(true);// True:图片停在动画结束位置animation.setDuration(150);mTabImg.startAnimation(animation);}@Overridepublic void onPageScrolled(int arg0, float arg1, int arg2) {}@Overridepublic void onPageScrollStateChanged(int arg0) {}}@Override    public boolean onKeyDown(int keyCode, KeyEvent event) {    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {  //获取 back键            if(menu_display){         //如果 Menu已经打开 ,先关闭Menu        menuWindow.dismiss();        menu_display = false;        }        else {        Intent intent = new Intent();            intent.setClass(MainWeixin.this,Exit.class);            startActivity(intent);        }    }        else if(keyCode == KeyEvent.KEYCODE_MENU){   //获取 Menu键if(!menu_display){//获取LayoutInflater实例inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);//这里的main布局是在inflate中加入的哦,以前都是直接this.setContentView()的吧?呵呵//该方法返回的是一个View的对象,是布局中的根layout = inflater.inflate(R.layout.main_menu, null);//下面我们要考虑了,我怎样将我的layout加入到PopupWindow中呢???很简单menuWindow = new PopupWindow(layout,LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); //后两个参数是width和height//menuWindow.showAsDropDown(layout); //设置弹出效果//menuWindow.showAsDropDown(null, 0, layout.getHeight());menuWindow.showAtLocation(this.findViewById(R.id.mainweixin), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置//如何获取我们main中的控件呢?也很简单mClose = (LinearLayout)layout.findViewById(R.id.menu_close);mCloseBtn = (LinearLayout)layout.findViewById(R.id.menu_close_btn);//下面对每一个Layout进行单击事件的注册吧。。。//比如单击某个MenuItem的时候,他的背景色改变//事先准备好一些背景图片或者颜色mCloseBtn.setOnClickListener (new View.OnClickListener() {@Overridepublic void onClick(View arg0) {//Toast.makeText(Main.this, "退出", Toast.LENGTH_LONG).show();Intent intent = new Intent();        intent.setClass(MainWeixin.this,Exit.class);        startActivity(intent);        menuWindow.dismiss(); //响应点击事件之后关闭Menu}});menu_display = true;}else{//如果当前已经为显示状态,则隐藏起来menuWindow.dismiss();menu_display = false;}return false;}    return false;    }//设置标题栏右侧按钮的作用public void btnmainright(View v) {  Intent intent = new Intent (MainWeixin.this,MainTopRightDialog.class);startActivity(intent);//Toast.makeText(getApplicationContext(), "点击了功能按钮", Toast.LENGTH_LONG).show();      }  public void startchat(View v) {      //小黑  对话界面Intent intent = new Intent (MainWeixin.this,ChatActivity.class);startActivity(intent);//Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_LONG).show();      }  public void exit_settings(View v) {                           //退出  伪“对话框”,其实是一个activityIntent intent = new Intent (MainWeixin.this,ExitFromSettings.class);startActivity(intent); }public void btn_shake(View v) {                                   //手机摇一摇Intent intent = new Intent (MainWeixin.this,ShakeActivity.class);startActivity(intent);}}        

对应布局文件:
main_weixin.xml
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/mainweixin"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"    android:background="#eee" >      <RelativeLayout        android:id="@+id/main_bottom"        android:layout_width="match_parent"        android:layout_height="55dp"        android:layout_alignParentBottom="true"        android:orientation="vertical"        android:background="@drawable/bottom_bar"        >                           <ImageView        android:id="@+id/img_tab_now"        android:layout_width="wrap_content"        android:layout_height="wrap_content"                     android:scaleType="matrix"        android:layout_gravity="bottom"                        android:layout_alignParentBottom="true"        android:src="@drawable/tab_bg" />                                         <LinearLayout            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_alignParentBottom="true"            android:paddingBottom="2dp"                    >                    <LinearLayout            android:layout_width="wrap_content"            android:layout_height="wrap_content"              android:gravity="center_horizontal"              android:orientation="vertical"              android:layout_weight="1">                               <ImageView            android:id="@+id/img_weixin"        android:layout_width="wrap_content"        android:layout_height="wrap_content"                     android:scaleType="matrix"        android:clickable="true"        android:src="@drawable/tab_weixin_pressed" />                <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="微信"            android:textColor="#fff"            android:textSize="12sp" />                         </LinearLayout>         <LinearLayout            android:layout_width="wrap_content"            android:layout_height="wrap_content"              android:gravity="center_horizontal"              android:orientation="vertical"              android:layout_weight="1">                               <ImageView            android:id="@+id/img_address"        android:layout_width="wrap_content"        android:layout_height="wrap_content"                     android:scaleType="matrix"        android:clickable="true"        android:src="@drawable/tab_address_normal" />                <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="通讯录"            android:textColor="#fff"            android:textSize="12sp" />                         </LinearLayout>         <LinearLayout            android:layout_width="wrap_content"            android:layout_height="wrap_content"              android:gravity="center_horizontal"              android:orientation="vertical"              android:layout_weight="1">                               <ImageView            android:id="@+id/img_friends"        android:layout_width="wrap_content"        android:layout_height="wrap_content"                     android:scaleType="matrix"        android:clickable="true"        android:src="@drawable/tab_find_frd_normal" />                <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="朋友们"            android:textColor="#fff"            android:textSize="12sp" />                         </LinearLayout>                  <LinearLayout            android:layout_width="wrap_content"            android:layout_height="wrap_content"              android:gravity="center_horizontal"              android:orientation="vertical"              android:layout_weight="1">                               <ImageView            android:id="@+id/img_settings"        android:layout_width="wrap_content"        android:layout_height="wrap_content"                     android:scaleType="matrix"        android:clickable="true"        android:src="@drawable/tab_settings_normal" />                <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="设置"            android:textColor="#fff"            android:textSize="12sp" />                         </LinearLayout>                          </LinearLayout>                </RelativeLayout>    <LinearLayout        android:layout_width="fill_parent"    android:layout_height="wrap_content"     android:layout_alignParentTop="true"    android:layout_above="@id/main_bottom"               android:orientation="vertical" >                <android.support.v4.view.ViewPager        android:id="@+id/tabpager"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center" >         </android.support.v4.view.ViewPager>      </LinearLayout>   </RelativeLayout>


   

4、实现导航的滑动功能(继承TabActivity)

package com.example.yqqmobilesafe;import android.app.TabActivity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.GestureDetector;import android.view.GestureDetector.SimpleOnGestureListener;import android.view.MotionEvent;import android.view.View;import android.widget.TabHost;/** * 骚扰拦截 * @author yqq * */public class StopAnonyActivity extends TabActivity {private TabHost mTabHost;private static final int SWIPE_MIN_DISTANCE = 120;      private static final int SWIPE_MAX_OFF_PATH = 250;      private static final int SWIPE_THRESHOLD_VELOCITY = 200;      private GestureDetector gestureDetector;      View.OnTouchListener gestureListener;          int currentView = 0;      private static int maxTabIndex = 2;public StopAnonyActivity() {}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);init(); gestureDetector = new GestureDetector(new MyGestureDetector());          gestureListener = new View.OnTouchListener() {              public boolean onTouch(View v, MotionEvent event) {                  if (gestureDetector.onTouchEvent(event)) {                      return true;                  }                  return false;              }          };  }/** * 初始化 */private void init() {setContentView(R.layout.activity_stop_annoy);mTabHost=getTabHost();Intent intent1=new Intent(StopAnonyActivity.this,RubishSmsActivity.class);mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("垃圾短信").setContent(intent1));Intent intent2=new Intent(StopAnonyActivity.this,AnnoyCallActivity.class);mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("骚扰电话").setContent(intent2));Intent intent3=new Intent(StopAnonyActivity.this,StopAnnoySettingActivity.class);mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("拦截设置").setContent(intent3));//从0开始//mTabHost.setCurrentTab(1);} // 左右滑动刚好页面也有滑动效果      class MyGestureDetector extends SimpleOnGestureListener {          @Override          public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,                  float velocityY) {              TabHost tabHost = getTabHost();                          try {                  if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)                      return false;                  // right to left swipe                  if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE                          && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {                      Log.i("test", "right");                      if (currentView == maxTabIndex) {                          currentView = 0;                      } else {                          currentView++;                      }                      tabHost.setCurrentTab(currentView);                  } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE                          && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {                      Log.i("test", "left");                      if (currentView == 0) {                          currentView = maxTabIndex;                      } else {                          currentView--;                      }                      tabHost.setCurrentTab(currentView);                  }              } catch (Exception e) {              }              return false;          }      }      public boolean dispatchTouchEvent(MotionEvent event) {          if (gestureDetector.onTouchEvent(event)) {              event.setAction(MotionEvent.ACTION_CANCEL);          }          return super.dispatchTouchEvent(event);      }    }

对应布局文件:
activity_stop_annoy.xml
<?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="match_parent"    android:layout_height="match_parent" >    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="match_parent" >        <TabWidget            android:background="@drawable/light_green"            android:id="@android:id/tabs"            android:layout_width="match_parent"            android:layout_height="50dp" >        </TabWidget>        <FrameLayout                     android:id="@android:id/tabcontent"            android:layout_width="match_parent"            android:layout_height="match_parent" >        </FrameLayout>    </RelativeLayout></TabHost>

效果图: android 导航总结_第1张图片

5、用FragmentActivity实现导航

public class MainActivity extends FragmentActivity{private ViewPager mViewPager;private FragmentPagerAdapter mAdapter;private List<Fragment> mFragments = new ArrayList<Fragment>();/** * 顶部三个LinearLayout */private LinearLayout mTabLiaotian;private LinearLayout mTabFaxian;private LinearLayout mTabTongxunlun;/** * 顶部的三个TextView */private TextView mLiaotian;private TextView mFaxian;private TextView mTongxunlu;/** * 分别为每个TabIndicator创建一个BadgeView */private BadgeView mBadgeViewforLiaotian;private BadgeView mBadgeViewforFaxian;private BadgeView mBadgeViewforTongxunlu;/** * Tab的那个引导线 */private ImageView mTabLine;/** * ViewPager的当前选中页 */private int currentIndex;/** * 屏幕的宽度 */private int screenWidth;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mViewPager = (ViewPager) findViewById(R.id.id_viewpager);initView();initTabLine();/** * 初始化Adapter */mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()){@Overridepublic int getCount(){return mFragments.size();}@Overridepublic Fragment getItem(int arg0){return mFragments.get(arg0);}};mViewPager.setAdapter(mAdapter);/** * 设置监听 */mViewPager.setOnPageChangeListener(new OnPageChangeListener(){@Overridepublic void onPageSelected(int position){//重置所有TextView的字体颜色resetTextView();switch (position){case 0:mTabLiaotian.removeView(mBadgeViewforLiaotian);mBadgeViewforLiaotian.setBadgeCount(5);mTabLiaotian.addView(mBadgeViewforLiaotian);mLiaotian.setTextColor(getResources().getColor(R.color.green));break;case 1:mFaxian.setTextColor(getResources().getColor(R.color.green));mTabFaxian.removeView(mBadgeViewforFaxian);mBadgeViewforFaxian.setBadgeCount(15);mTabFaxian.addView(mBadgeViewforFaxian);break;case 2:mTongxunlu.setTextColor(getResources().getColor(R.color.green));break;}currentIndex = position;}@Overridepublic void onPageScrolled(int position, float positionOffset, int positionOffsetPixels){/** * 利用position和currentIndex判断用户的操作是哪一页往哪一页滑动 * 然后改变根据positionOffset动态改变TabLine的leftMargin */if (currentIndex == 0 && position == 0)// 0->1{LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) mTabLine.getLayoutParams();lp.leftMargin = (int) (positionOffset * (screenWidth * 1.0 / 3) + currentIndex * (screenWidth / 3));mTabLine.setLayoutParams(lp);} else if (currentIndex == 1 && position == 0) // 1->0{LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) mTabLine.getLayoutParams();lp.leftMargin = (int) (-(1 - positionOffset) * (screenWidth * 1.0 / 3) + currentIndex* (screenWidth / 3));mTabLine.setLayoutParams(lp);} else if (currentIndex == 1 && position == 1) // 1->2{LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) mTabLine.getLayoutParams();lp.leftMargin = (int) (positionOffset * (screenWidth * 1.0 / 3) + currentIndex * (screenWidth / 3));mTabLine.setLayoutParams(lp);} else if (currentIndex == 2 && position == 1) // 2->1{LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) mTabLine.getLayoutParams();lp.leftMargin = (int) (-(1 - positionOffset) * (screenWidth * 1.0 / 3) + currentIndex* (screenWidth / 3));mTabLine.setLayoutParams(lp);}}@Overridepublic void onPageScrollStateChanged(int state){}});mViewPager.setCurrentItem(1);}/** * 根据屏幕的宽度,初始化引导线的宽度 */private void initTabLine(){mTabLine = (ImageView) findViewById(R.id.id_tab_line);DisplayMetrics outMetrics = new DisplayMetrics();getWindow().getWindowManager().getDefaultDisplay().getMetrics(outMetrics);screenWidth = outMetrics.widthPixels;LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) mTabLine.getLayoutParams();lp.width = screenWidth / 3;mTabLine.setLayoutParams(lp);}/** * 重置颜色 */protected void resetTextView(){mLiaotian.setTextColor(getResources().getColor(R.color.black));mFaxian.setTextColor(getResources().getColor(R.color.black));mTongxunlu.setTextColor(getResources().getColor(R.color.black));}/** * 初始化控件,初始化Fragment */private void initView(){mTabLiaotian = (LinearLayout) findViewById(R.id.id_tab_liaotian_ly);mTabFaxian = (LinearLayout) findViewById(R.id.id_tab_faxian_ly);mTabTongxunlun = (LinearLayout) findViewById(R.id.id_tab_tongxunlu_ly);mLiaotian = (TextView) findViewById(R.id.id_liaotian);mFaxian = (TextView) findViewById(R.id.id_faxian);mTongxunlu = (TextView) findViewById(R.id.id_tongxunlu);MainTab01 tab01 = new MainTab01();MainTab02 tab02 = new MainTab02();MainTab03 tab03 = new MainTab03();mFragments.add(tab01);mFragments.add(tab02);mFragments.add(tab03);mBadgeViewforFaxian = new BadgeView(this);mBadgeViewforLiaotian = new BadgeView(this);mBadgeViewforTongxunlu = new BadgeView(this);}}

对应布局文件:


更多相关文章

  1. 文件对应
  2. Android 的网络编程(8)-文件上传功能
  3. Android MainActivity如何加载BaseActivity的布局
  4. Android实时抓取日志,生成文件
  5. Android Layout Tricks #1(Android 布局技巧1)
  6. android保存第一次安装状态并初始化数据
  7. Android之Fragment界面布局实例
  8. android assets/raw 大文件读取

随机推荐

  1. Android手动签名APK
  2. Android(安卓)2016年你需要去掌握的技术,
  3. 让Android设备永不休眠
  4. Android(安卓)椭圆路径 长按暂停动画的实
  5. android 中无法使用LOGE显示log
  6. 文本中的值的保存与恢复
  7. Android(安卓)Studio jcenter集成butterk
  8. Android(安卓)studio使用教程
  9. 1 android 人脸识别
  10. Android(安卓)Content Provider Guides