这里实现选项卡是使用android提供的原生的tabHost和tabWidget两个控件完成的,在使用的时候,注意在定义id的时候,对于TabHost和TabWidget两个控件的Id是不能少的。这里使用的是android:id的形式进行定义id的,这样定义id和使用android:@+id的原理是一样的,只不过在actiivyt中的使用时。一个是使用R.id.xx,这里的R文件就是关于我们所创建的包来命名的,另一个是使用android.R.id.xxx,这种使用表明的是使用android自带的控件。
其次,就是创建相对应的切换布局,这里创建的切换布局都是使用的lIneLayout布局实现的,每种布局都是对应的ListView,这样一共创建了4个,而且每一种都有相对应的id。
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:layout_alignParentLeft="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="32dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="top">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFF7"
android:orientation="horizontal">
<ListView
android:id="@+id/fishLocalList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp">
</ListView>
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFF7"
android:orientation="horizontal">
<ListView
android:id="@+id/jiakeLocalList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp">
</ListView>
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFF7"
android:orientation="horizontal">
<ListView
android:id="@+id/ruantiLocalList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp">
</ListView>
</LinearLayout>
<LinearLayout
android:id="@+id/tab4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFF7"
android:orientation="horizontal">
<ListView
android:id="@+id/zaoleiLocalList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp">
</ListView>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
1) 在java代码中实现
TabHost tabHost;
View child1,child2,child3,child4;
ListView fishLocalList,jiakeLocalList,ruantiLocalList,zaoleiLocalList;
public void
initData(){
tabHost = (TabHost) findViewById(android.R.id.tabhost);
fishLocalList=(ListView)findViewById(R.id.fishLocalList);
jiakeLocalList=(ListView)findViewById(R.id.jiakeLocalList);
ruantiLocalList=(ListView)findViewById(R.id.ruantiLocalList);
zaoleiLocalList=(ListView)findViewById(R.id.zaoleiLocalList);
}
public void initlickFishList(){
clickList(fishLocalList);
clickList(jiakeLocalList);
clickList(ruantiLocalList);
clickList(zaoleiLocalList);
}
public void initTabXuanxiangka(){
tabHost.setup();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1").setContent(R.id.tab1));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("tab2").setContent(R.id.tab2));
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab3").setContent(R.id.tab3));
tabHost.addTab(tabHost.newTabSpec("tab4").setIndicator("tab4").setContent(R.id.tab4));
child1= tabHost.getTabWidget().getChildAt(0);
child2= tabHost.getTabWidget().getChildAt(1);
child3= tabHost.getTabWidget().getChildAt(2);
child4= tabHost.getTabWidget().getChildAt(3);
initTabWidget(tabHost.getTabWidget(),0,R.drawable.tab_fish_xuanxiangka_drawable);
initTabWidget(tabHost.getTabWidget(),1,R.drawable.tab_jiake_xuanxiangka_drawable);
initTabWidget(tabHost.getTabWidget(),2,R.drawable.tab_ruanti_xuanxiangka_drawable);
initTabWidget(tabHost.getTabWidget(),3,R.drawable.tab_zaolei_xuanxiangka_drawable);
}
public void initTabWidget(TabWidget tabWidget,int
index,int drawableResource){
LinearLayout.LayoutParams lp=(LinearLayout.LayoutParams)tabWidget.getChildAt(index).getLayoutParams();
tabWidget.getChildAt(index).setBackgroundResource(drawableResource);
lp.width = LinearLayout.LayoutParams.MATCH_PARENT;
lp.height =
160;
lp.weight =
0.5f;
tabWidget.getChildAt(0).setBackgroundResource(R.drawable.fish_on);
tabWidget.getChildAt(1).setBackgroundResource(R.drawable.jiake_off);
tabWidget.getChildAt(2).setBackgroundResource(R.drawable.ruanti_off);
tabWidget.getChildAt(3).setBackgroundResource(R.drawable.zao_off);
tabHost.setOnTabChangedListener(new
TabHost.OnTabChangeListener() {
@Override
public void
onTabChanged(String tabId) {
if(tabId.equals("tab1")){
child1.setBackgroundResource(R.drawable.fish_on);
child2.setBackgroundResource(R.drawable.jiake_off);
child3.setBackgroundResource(R.drawable.ruanti_off);
child4.setBackgroundResource(R.drawable.zao_off);
}else if(tabId.equals("tab2")){
child1.setBackgroundResource(R.drawable.fish_off);
child2.setBackgroundResource(R.drawable.jiake_on);
child3.setBackgroundResource(R.drawable.ruanti_off);
child4.setBackgroundResource(R.drawable.zao_off);
}else if(tabId.equals("tab3")){
child1.setBackgroundResource(R.drawable.fish_off);
child2.setBackgroundResource(R.drawable.jiake_off);
child3.setBackgroundResource(R.drawable.ruanti_on);
child4.setBackgroundResource(R.drawable.zao_off);
}else if(tabId.equals("tab4")){
child1.setBackgroundResource(R.drawable.fish_off);
child2.setBackgroundResource(R.drawable.jiake_off);
child3.setBackgroundResource(R.drawable.ruanti_off);
child4.setBackgroundResource(R.drawable.zao_on);
}
}
});
}
2) 注意点
在实现选项卡的时候,一定要注意要给TabHost和TabWidget加上id,而且要加的id都是必须的。