这个主要是实现底部的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));
}
}
}
});
}
}
}
内容版权声明:除非注明,否则皆为本站原创文章。
转载注明出处:http://www.heiqu.com/psdsz.html