【原创】android中实现底部tabhost

这个主要是实现底部的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));
						}
					}
				}
			});
		}
	}
}


这个是主类
再看看主布局文件:
<?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">
   
   <LinearLayout 
  	  android:orientation="vertical"
  	  android:layout_width="fill_parent"
   	  android:layout_height="fill_parent">
      <FrameLayout
         android:id="@android:id/tabcontent"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_weight="1">
      </FrameLayout>
      <TabWidget
         android:id="@android:id/tabs"
         android:layout_width="fill_parent"
         android:layout_alignParentBottom="true"
         android:layout_height="wrap_content"/>  
	</LinearLayout>
</TabHost>

这样,基本上一个可用的tabhost就可以了。
附件是代码

你可能感兴趣的:(android)