【原创】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就可以了。
附件是代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值