TabHostActivity.java代码:

package com.jswjtu.imageclassification.activity;import com.jswjtu.imageclassification.R;import android.app.TabActivity;import android.content.Intent;import android.content.res.Resources;import android.os.Bundle;import android.util.DisplayMetrics;import android.view.GestureDetector;import android.view.GestureDetector.SimpleOnGestureListener;import android.view.LayoutInflater;import android.view.MotionEvent;import android.view.Window;import android.widget.RelativeLayout;import android.widget.TabHost;import android.widget.TextView;@SuppressWarnings("deprecation")public class TabHostActivity extends TabActivity {    Intent intent;    TabHost.TabSpec tabSpec;    TabHost tabHost;    int currentTabID = 0;    int flaggingWidth;// 互动翻页所需滚动的长度是当前屏幕宽度的1/3    GestureDetector gestureDetector        = new GestureDetector(new TabHostTouch());    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);         //设置无标题         requestWindowFeature(Window.FEATURE_NO_TITLE);          setContentView(R.layout.tabhost);        Resources res = getResources();        tabHost = getTabHost();        DisplayMetrics dm = new DisplayMetrics();        getWindowManager().getDefaultDisplay().getMetrics(dm);        flaggingWidth = dm.widthPixels / 3;        //给Tab1添加自定义样式         RelativeLayout tabStyle1 = (RelativeLayout)LayoutInflater.from(this).inflate(R.layout.tab_style, null);          TextView text1 = (TextView)tabStyle1.findViewById(R.id.tab_label);          text1.setText("相册");          //给Tab2添加自定义样式         RelativeLayout tabStyle2 = (RelativeLayout)LayoutInflater.from(this).inflate(R.layout.tab_style, null);          TextView text2 = (TextView)tabStyle2.findViewById(R.id.tab_label);          text2.setText("视频");          intent = new Intent(TabHostActivity.this,ImageActivity.class);//指定tab页面的Activity        tabSpec = tabHost.newTabSpec("image"); // 创建一个新的标签页,标记为“image”        tabSpec.setIndicator(tabStyle1);// 设置tab页的名称和图像表示        tabSpec.setContent(intent);// 设置此tab跳转到的Activity        tabHost.addTab(tabSpec);// 将此tab加入到tabHost        intent = new Intent(TabHostActivity.this,VideoActivity.class);        tabSpec = tabHost.newTabSpec("video");        tabSpec.setIndicator(tabStyle2);        tabSpec.setContent(intent);        tabHost.addTab(tabSpec);        tabHost.setCurrentTab(0);// 设置当期的tab页,从0开始偏移    }    @Override      public boolean dispatchTouchEvent(MotionEvent event) {          if (gestureDetector.onTouchEvent(event)) {              event.setAction(MotionEvent.ACTION_CANCEL);          }          return super.dispatchTouchEvent(event);      }     private class TabHostTouch extends SimpleOnGestureListener {          @Override          public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,                  float velocityY) {              if (Math.abs(e1.getX() - e2.getX()) > Math.abs(e1.getY()                      - e2.getY())                      && (e1.getX() - e2.getX() <= (-flaggingWidth) || e1.getX()                              - e2.getX() >= flaggingWidth)) {                  currentTabID = tabHost.getCurrentTab();                if (e1.getX() - e2.getX() <= (-flaggingWidth)) {                      if (tabHost.getCurrentTab() == 1) {                          currentTabID = 0;                    }                      tabHost.setCurrentTab(currentTabID);                      return true;                  } else if (e1.getX() - e2.getX() >= flaggingWidth) {                      if (tabHost.getCurrentTab() == 0) {                          currentTabID = 1;                    }                      tabHost.setCurrentTab(currentTabID);                      return true;                  }              }              return false;          }      }  }

tabhost.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" > <!-- 注意这里,id的设置方法跟普通控件不同,必须为tabhost -->    <LinearLayout  android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >        <TabWidget  android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" />        <FrameLayout  android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="wrap_content" />    </LinearLayout></TabHost>

tab_style.xml代码:

<?xml version="1.0" encoding="UTF-8"?>  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="5dip" android:paddingRight="5dip" android:background="#67e9bb" >        <TextView android:id="@+id/tab_label" android:layout_width="fill_parent" android:layout_height="40dp" android:gravity="center" android:textColor="#000000" android:textStyle="bold" android:background="@drawable/tab" />   </RelativeLayout>  

tab.xml代码:

<?xml version="1.0" encoding="UTF-8"?>  <selector xmlns:android = "http://schemas.android.com/apk/res/android">              <item android:state_selected = "true" android:drawable="@drawable/tab_sel_ture" />         <item android:state_selected = "false" android:drawable="@drawable/tab_sel_false" />      </selector>  

即可;

更多相关文章

  1. android 开源项目(城市定位)
  2. Android(安卓)SeekBar(拖动条)
  3. Android(安卓)WebView文件上传(关键代码)
  4. android 调用摄像头
  5. android setPersistent
  6. android,java代码设置背景色
  7. Android(安卓)widget使用
  8. AutoCompleteTextView的基本使用
  9. Android(安卓)Studio控件技巧汇总

随机推荐

  1. Android(安卓)Studio 主题如何黑色变为白
  2. 【Android】Menu详解
  3. Android(安卓)Studio 3.6.2 | 使用内置文
  4. android ctivity完美退出
  5. Android(安卓)自定义底部上拉控件的实现
  6. Android(安卓)在Launcher桌面添加应用快
  7. 一个有趣的android加载动画
  8. 细看 Google+ 里最有希望成为杀手级应用
  9. Android(安卓)9.0实现系统内录(只录制系统
  10. 关于Android中的Intent一点说明