這個主要是實現底部的tabhost方式,tabhost就是有幾個標簽滑動的一個控件。activity繼承TabActivity
其他不多說了,直接上代碼
- public class main extends TabActivity {
- private TabHost tabHost;
- private TabWidget tabWidget;
- Field mBottomLeftStrip;
- Field mBottomRightStrip;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- makeTab();
- }
-
- public void makeTab() {
- if (this.tabHost == null) {
- tabHost = getTabHost();
- tabWidget = getTabWidget();
- tabHost.setup();
- tabHost.bringToFront();
-
- TabSpec firsttab = tabHost.newTabSpec("firsttab");
- TabSpec sencondtab = tabHost.newTabSpec("sencondtab");
- TabSpec thirdtab = tabHost.newTabSpec("thirdtab");
-
- firsttab.setIndicator("first",
- getResources().getDrawable(R.drawable.first)).setContent(
- new Intent(this, first.class));
- sencondtab.setIndicator("second",
- getResources().getDrawable(R.drawable.second)).setContent(
- new Intent(this, second.class));
- thirdtab.setIndicator("third",
- getResources().getDrawable(R.drawable.third)).setContent(
- new Intent(this, third.class));
-
- tabHost.addTab(firsttab);
- tabHost.addTab(sencondtab);
- tabHost.addTab(thirdtab);
-
- if (Integer.valueOf(Build.VERSION.SDK) <= 7) {
- try {
- mBottomLeftStrip = tabWidget.getClass().getDeclaredField(
- "mBottomLeftStrip");
- mBottomRightStrip = tabWidget.getClass().getDeclaredField(
- "mBottomRightStrip");
- if (!mBottomLeftStrip.isAccessible()) {
- mBottomLeftStrip.setAccessible(true);
- }
- if (!mBottomRightStrip.isAccessible()) {
- mBottomRightStrip.setAccessible(true);
- }
- mBottomLeftStrip.set(tabWidget,
- getResources().getDrawable(R.drawable.linee));
- mBottomRightStrip.set(tabWidget, getResources()
- .getDrawable(R.drawable.linee));
-
- } catch (Exception e) {
- e.printStackTrace();
- }
- } else {
- try {
- mBottomLeftStrip = tabWidget.getClass().getDeclaredField(
- "mLeftStrip");
- mBottomRightStrip = tabWidget.getClass().getDeclaredField(
- "mRightStrip");
- if (!mBottomLeftStrip.isAccessible()) {
- mBottomLeftStrip.setAccessible(true);
- }
- if (!mBottomRightStrip.isAccessible()) {
- mBottomRightStrip.setAccessible(true);
- }
- mBottomLeftStrip.set(tabWidget,
- getResources().getDrawable(R.drawable.linee));
- mBottomRightStrip.set(tabWidget, getResources()
- .getDrawable(R.drawable.linee));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- for (int i = 0; i < tabWidget.getChildCount(); i++) {
-
- View view = tabWidget.getChildAt(i);
- if (tabHost.getCurrentTab() == i) {
- view.setBackgroundDrawable(getResources().getDrawable(
- R.drawable.focus));
- } else {
- view.setBackgroundDrawable(getResources().getDrawable(
- R.drawable.unfocus));
- }
- }
-
- tabHost.setOnTabChangedListener(new OnTabChangeListener() {
-
- @Override
- public void onTabChanged(String tabId) {
- for (int i = 0; i < tabWidget.getChildCount(); i++) {
- View view = tabWidget.getChildAt(i);
- Toast.makeText(main.this, tabId, Toast.LENGTH_SHORT).show();
- if (tabHost.getCurrentTab() == i) {
- view.setBackgroundDrawable(getResources()
- .getDrawable(R.drawable.focus));
- } else {
- view.setBackgroundDrawable(getResources()
- .getDrawable(R.drawable.unfocus));
- }
- }
- }
- });
- }
- }
- }