Tabhost

TabHost自定义图标的应用2010-12-24 20:31:05|  分类: android |  标签:android  tabhost  自定义图片     字号:大中小 订阅
关于tabhost的应用已经很多了,今天我要写的是怎么改变tabhost的样式。
首先展示一下效果图:




public class MainActivity extends Activity {
//首先不要继承tabActivity,那样我们就不能自己创建tabhost了。
private TabHost mTabHost;
private View categoryView;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTabHost = (TabHost) findViewById(R.id.tabhost);
mTabHost.setup();//创建tabhost


LayoutInflater inflater = LayoutInflater.from(this);
categoryView = inflater.inflate(R.layout.category, mTabHost.getTabContentView());
inflater.inflate(R.layout.tab2, mTabHost.getTabContentView());
inflater.inflate(R.layout.search, mTabHost.getTabContentView());


mTabHost.addTab(mTabHost.newTabSpec("t1").setIndicator("")
.setContent(R.id.LinearLayout01));
mTabHost.addTab(mTabHost.newTabSpec("t2").setIndicator("")
.setContent(R.id.FrameLayout02));
mTabHost.addTab(mTabHost.newTabSpec("t3").setIndicator("")
.setContent(R.id.LinearLayout03));

TabWidget tw = mTabHost.getTabWidget();
//tw.setStripEnabled(false);
//设置tabhost的图片
mTabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.category_bottom);
mTabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.buycar_bottom);
mTabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.search_bottom);

}
}


main.xml源码,一定要注意每个控件的名称


<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- android:paddingBottom="50dip" 避免覆盖TabWidget -->
<FrameLayout android:id="@android:id/tabcontent" android:paddingBottom="50dip"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</FrameLayout>


<TabWidget android:id="@android:id/tabs" android:layout_gravity="bottom"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</TabHost> 

猜你喜欢

转载自xblia.iteye.com/blog/1107000