簡(jiǎn)介
Container for a tabbed window view. This object holds two children: a set of tab labels that theuser clicks to select a specific tab, and a FrameLayout object that displays the contents of thatpage. The individual elements are typically controlled using this container object, rather thansetting values on the child elements themselves.
TabHost 犀斋,標(biāo)簽視圖的容器。容器包含兩個(gè)孩子節(jié)點(diǎn)搔弄,一個(gè)用來(lái)存放一系列的標(biāo)簽,點(diǎn)擊來(lái)選擇對(duì)應(yīng)的窗口椿疗;一個(gè)是FrameLayout用來(lái)存放頁(yè)面具體內(nèi)容萍虽。這些獨(dú)立的元素通常用TabHost來(lái)控制轴脐,而不是在視圖內(nèi)部通過(guò)設(shè)置值來(lái)實(shí)現(xiàn)
類結(jié)構(gòu)
方法 | 意義 |
---|---|
addTab | 添加一個(gè)tab |
clearAllTabs | 移除所有的tab |
dispatchKeyEvent | 下發(fā)keyevent |
dispatchWindowFocusChanged | 下發(fā)windowsfocusChanged事件 |
newTabSpec | 創(chuàng)建一個(gè)新的TabSpec,關(guān)聯(lián)到具體內(nèi)容 |
onTouchModeChanged | NA |
setup() | 不和TabActivity關(guān)聯(lián)砾嫉,通過(guò)findViewById獲取的TabHost需要調(diào)用setup(),如果是在TabActivity中通過(guò)getTabHost()的方式獲取的不需要調(diào)用這個(gè)方法 |
setup(LocalActivityManager activityGroup) | setContent中傳入intent的時(shí)候才關(guān)注下這個(gè)方法 |
getCurrentTab()/setCurrentTab() | 獲取/設(shè)置當(dāng)前需要顯示的tab幼苛,通過(guò)index |
setCurrentTabByTag/getCurrentTabTag | 通過(guò)tag設(shè)置當(dāng)前需要顯示的Tab,tag就是創(chuàng)建TabSpec的時(shí)候傳入的字符串 |
getCurrentTabView | 設(shè)置/獲取當(dāng)前在TabWidget中顯示的View,也就是作為標(biāo)簽的View而非內(nèi)容 |
getCurrentView | 獲取當(dāng)前顯示的內(nèi)容 |
setOnTabChangedListener | 設(shè)置標(biāo)簽頁(yè)切換事件監(jiān)聽(tīng) |
getTabContentView | 獲取內(nèi)容頁(yè)面的容器FrameLayout |
getTabWidget | 獲取TabWidget |
基本使用
1. 布局文件(content_fragment.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorTopBackGround">
</android.support.v7.widget.Toolbar>
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 設(shè)置的id必須是 "@android:id/tabcontent"-->
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9">
<TextView android:id="@+id/tv_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is one"
android:textColor="@color/colorAccent"
android:textSize="20sp" />
<TextView android:id="@+id/tv_two"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is two"
android:textColor="@color/colorPrimary"
android:textSize="25sp" />
<TextView android:id="@+id/tv_three"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is three"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp" />
<TextView android:id="@+id/tv_four"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is Four"
android:textColor="@color/colorBlack"
android:textSize="35sp" />
</FrameLayout>
<!-- 設(shè)置的id必須是android:id="@android:id/tabs" -->
<TabWidget android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/colorWhite"
android:padding="5dp"
android:showDividers="none">
</TabWidget>
</LinearLayout>
</TabHost>
</LinearLayout>
2. 自定義底部標(biāo)簽布局(myindicator.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView android:id="@+id/iv_indicator"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5">
</ImageView>
<TextView android:id="@+id/tv_indicator"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2.5"
android:gravity="center" android:textSize="5pt" />
</LinearLayout>
底部一個(gè)圖標(biāo)下面一段文字
3. 代碼使用(MainActivity.java),不使用TabActivity
public class MainActivity extends AppCompatActivity {
float initx = 0.0f, currentx = 0.0f;
TabHost tabHost = null;
Toolbar toolbar = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_fragment);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);//設(shè)置ActionBar
toolbar.setTitleTextColor(getResources().getColor(R.color.colorWhite));//設(shè)置背景色
tabHost = (TabHost) findViewById(R.id.tabhost);
tabHost.setup();//初始化TabHost
/*添加tab*/
for (int i = 0; i < 4; i++) {
View view = LayoutInflater.from(this).inflate(R.layout.myindicator, null, false);
TextView textView = (TextView) view.findViewById(R.id.tv_indicator);
ImageView imageView = (ImageView) view.findViewById(R.id.iv_indicator);
switch (i) {
case 0:
textView.setText("微信");
imageView.setImageResource(R.drawable.weixin);
tabHost.addTab(tabHost.newTabSpec("1").setIndicator(view).setContent(R.id.tv_one));
break;
case 1:
textView.setText("通訊錄");
imageView.setImageResource(R.drawable.contact);
tabHost.addTab(tabHost.newTabSpec("2").setIndicator(view).setContent(R.id.tv_two));
break;
case 2:
textView.setText("發(fā)現(xiàn)");
imageView.setImageResource(R.drawable.find);
tabHost.addTab(tabHost.newTabSpec("3").setIndicator(view).setContent(R.id.tv_three));
break;
case 3:
textView.setText("我");
imageView.setImageResource(R.drawable.profile);
tabHost.addTab(tabHost.newTabSpec("4").setIndicator(view).setContent(R.id.tv_four));
break;
}
}
/*設(shè)置標(biāo)簽切換監(jiān)聽(tīng)器*/
tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
for (int i = 0; i < 4; i++) {//顏色全部重置
((TextView) tabHost.getTabWidget().getChildTabViewAt(i).findViewById(R.id.tv_indicator)).setTextColor(getResources().getColor(R.color.colorBlack));
}
if (tabHost.getCurrentTabTag() == tabId) {
((TextView) tabHost.getCurrentTabView().findViewById(R.id.tv_indicator)).setTextColor(getResources().getColor(R.color.colorSelected));
}//選中的那個(gè)Tab文字顏色修改
}
});
((TextView) tabHost.getCurrentTabView().findViewById(R.id.tv_indicator)).setTextColor(getResources().getColor(R.color.colorSelected));//第一次進(jìn)入的時(shí)候講選中的Tab修改文字顏色
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
initx = event.getX();
break;
case MotionEvent.ACTION_MOVE:
currentx = event.getX();
break;
case MotionEvent.ACTION_UP:
/*左右滑動(dòng)事件處理*/
if ((currentx - initx) > 25) {
if (tabHost.getCurrentTab() != 0) { tabHost.setCurrentTab(tabHost.getCurrentTab() - 1);
}
} else if ((currentx - initx) < -25) {
if (tabHost.getCurrentTab() != tabHost.getTabContentView().getChildCount()) {
tabHost.setCurrentTab(tabHost.getCurrentTab() + 1);
}
}
break;
}
return true;
}
/*菜單創(chuàng)建*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar, menu);
return true;
}
}
4. 代碼使用(MainActivity.java)焕刮,繼承TabActivity
TabActivity已經(jīng)被廢棄很久了舶沿,但是還是可以使用舌剂,在布局中將TabHost的id改成android:id="@android:id/tabhost",然后在繼承了TabActivity的MainActivity.java中使用tabHost = getTabHost();然后基本使用方法就和上面一樣了
實(shí)際效果
寫在最后
TabHost在TabActivity中的使用現(xiàn)在開(kāi)發(fā)過(guò)程中使用的不多暑椰,推薦使用ViewPager和Fragment的方式
我的CSDN博客地址:
http://blog.csdn.net/poorkick/article/details/53394047