1、布局界面:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EBF1F1"
android:orientation="vertical" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:showDividers="none"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
2、tab选项卡布局
<?xml version="1.0" encoding="utf-8"?>3、主activity的代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="40dip"
android:orientation="vertical" >
<TextView
android:id="@+id/tab_name"
android:layout_width="match_parent"
android:layout_height="35dip"
android:background="@color/blue_shallow"
android:gravity="center"
android:textSize="15sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="5dip"
android:background="@color/gray_line"
android:orientation="vertical" >
<ImageView
android:id="@+id/tab_line"
android:layout_width="match_parent"
android:layout_height="5dip"
android:background="#FF9900"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
public class TestActivity extends TabActivity {
protected TabHost tabHost;
private static final String TAB_PAGE1="page1";
private static final String TAB_PAGE2="page2";
private static final String TAB_PAGE3="page3";
private static final String TAB_PAGE4="page4";
private int tab_last_position;
//通过TabWidget可以获取选项卡的布局
private TabWidget mTabWidget;
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;
private static int maxTabIndex = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.apply_main);
tabHost = getTabHost();
TabSpec tabSpec = tabHost.newTabSpec(TAB_PAGE1);
tabSpec.setIndicator(createTabView("音乐"));
Intent intent = new Intent(this, GovernmentActivity.class);
tabSpec.setContent(intent);
tabHost.addTab(tabSpec);
tabSpec = tabHost.newTabSpec(TAB_PAGE2);
intent = new Intent(this, ClassifyActivity.class);
tabSpec.setContent(intent);
tabSpec.setIndicator(createTabView("电影"));
tabHost.addTab(tabSpec);
tabSpec = tabHost.newTabSpec(TAB_PAGE3);
tabSpec.setIndicator(createTabView("电视剧"));
intent = new Intent(this, PaihangActivity.class);
tabSpec.setContent(intent);
tabHost.addTab(tabSpec);
//
tabSpec = tabHost.newTabSpec(TAB_PAGE4);
tabSpec.setIndicator(createTabView("书籍"));
intent = new Intent(this, FirstTestActivity.class);
tabSpec.setContent(intent);
tabHost.addTab(tabSpec);
//默认使其选中
tabHost.setCurrentTab(0);
tab_last_position=0;
View view = tabHost.getCurrentTabView();
ImageView imageView = (ImageView) view.findViewById(R.id.tab_line);
TextView mTextView = (TextView) view.findViewById(R.id.tab_name);
imageView.setVisibility(View.VISIBLE);
mTextView.setTextColor(Color.GREEN);
mTabWidget = tabHost.getTabWidget();
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
ImageView imageView = null;
Logger.getLogger().i("------>" + tab_last_position);
//获取到视图,可以对其进行样式的改变
View lastView = mTabWidget.getChildAt(tab_last_position);
imageView = (ImageView) lastView.findViewById(R.id.tab_line);
TextView mTextView = (TextView) lastView.findViewById(R.id.tab_name);
if (imageView!=null) {
imageView.setVisibility(View.GONE);
mTextView.setTextColor(getResources().getColor(R.color.white));
}
View view = tabHost.getCurrentTabView();
imageView = (ImageView) view.findViewById(R.id.tab_line);
mTextView = (TextView) view.findViewById(R.id.tab_name);
imageView.setVisibility(View.VISIBLE);
mTextView.setTextColor(Color.GREEN);
mTabWidget = tabHost.getTabWidget();
tab_last_position = tabHost.getCurrentTab();
}
});
//滑动事件
gestureDetector = new GestureDetector(new TabGestureDetector());
// gestureListener = new View.OnTouchListener() {
// public boolean onTouch(View v, MotionEvent event) {
// if (gestureDetector.onTouchEvent(event)) {
// return true;
// }
// return false;
// }
// };
}
// 创建tab标签
protected View createTabView(String name) {
View tabView = getLayoutInflater().inflate(R.layout.tab_background, null);
TextView textView = (TextView) tabView.findViewById(R.id.tab_name);// 找到textview控件
textView.setText(name);
return tabView;
}
// 左右滑动刚好页面也有滑动效果
private class TabGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
int currentView = tabHost.getCurrentTab();
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.i("test", "right" +currentView);
if (currentView<maxTabIndex) {
currentView++;
tabHost.setCurrentTab(currentView);
}
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.i("test", "left" +currentView);
if (currentView > 0) {
currentView--;
tabHost.setCurrentTab(currentView);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
Log.i("test", "---------------dispatch ... ");
event.setAction(MotionEvent.ACTION_CANCEL);
}
return super.dispatchTouchEvent(event);
}
}