歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android TabHost 詳細講解

Android TabHost 詳細講解

日期:2017/3/1 10:16:53   编辑:Linux編程
一、什麼是TabHost。
Android 裡面的TabHost就是選項卡,看下圖(新浪微博界面):


至於選項卡有什麼好處或者用途,我想代碼哥們都知道吧,我就不多說了。

二、在Android裡面如何實現TabHost
有兩種方式可以實現。

1、繼承TabActivity,然後用getTabHost()方法獲取TabHost,最後在布局文件中定義各個Tab選項卡添加到TabHost中




2、不繼承TabActivity,然後在布局文件中定義TabHost,最後講各個Tab選項卡添加到TabHost中


總結以上兩種方式為兩步:

①:獲取TabHost對象

②:把Tab添加到TabHost中。


我們先看第一種實現:

①:布局文件:

<?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">


<!-- 第一個選項卡面板 -->
<LinearLayout
android:id="@+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<!-- 面板中只有一個TextView-->
<TextView android:id="@+id/V1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Touch Android"/>

</LinearLayout>


<!-- 第二個選項卡面板 -->
<LinearLayout
android:id="@+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<!-- 面板中只有一個TextView-->
<TextView android:id="@+id/V2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Touch Android"/>

</LinearLayout>

</LinearLayout>


②:Activity


/**
*Demo2Activity.java
*2011-9-17 下午12:07:48
*Touch Android
*http://bbs.droidstouch.com
*/
package com.droidstouch.tabhost;

import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

/**
* @author <a href="http://bbs.droidstouch.com">Touch Android</a>
*
*/
public class Demo2Activity extends TabActivity
{



protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

// this.setContentView(R.layout.demo2); // 注意不要加上此行代碼

//獲取到TabHost對象
TabHost tabHost =this.getTabHost();

//把我們的布局文件添加到tabHost 的FrameLayout下面
LayoutInflater.from(this).inflate(R.layout.demo2, tabHost.getTabContentView(), true);



// 下面定義了兩個選項卡

//獲取一個新的TabHost.TabSpec,並關聯到當前tab host
//參數:所需的選項卡標簽
TabSpec pSpec = tabHost.newTabSpec("parent");
// 參數一:選項卡上的文字,參數二:選項卡的背景圖片
pSpec.setIndicator("父類", this.getResources().getDrawable(R.drawable.f_root));
//設置選項卡內容
pSpec.setContent(R.id.tab1);


TabSpec subSpec = tabHost.newTabSpec("sub");
subSpec.setIndicator("子類", this.getResources().getDrawable(R.drawable.f_sub));
subSpec.setContent(R.id.tab2);


// 將選項卡添加到TabHost中
tabHost.addTab(pSpec);
tabHost.addTab(subSpec);

}

}

第二種方式
①:布局文件


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>



<!-- 定義TabHost ,自定義的TabHost一定得包含TabWidget 和 FrameLayout,
並且 TabWidget 的ID一定是@android:id/tabs
FrameLayout 的Id一定是@android:id/tabcontent
-->
<TabHost android:id="@+id/tabs"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<!-- 定義TabWidget,此控件 必須和TabHost一起使用 -->
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>

<!-- 定義FrameLayout-->
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView android:id="@+id/txtV1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Touch Android"/>


<TextView android:id="@+id/txtV2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="http://bbs.droidstouch.com"/>


</FrameLayout>


</LinearLayout>
</TabHost>

</LinearLayout>


②:Activity:

package com.droidstouch.tabhost;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class Dome1Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo1);




//從布局文件中 獲取到TabHost
TabHost tabHost = (TabHost) this.findViewById(R.id.tabs);
//安裝TabHost
tabHost.setup();


// 下面定義兩個選項卡

//獲取一個新的TabHost.TabSpec,並關聯到當前tab host
//參數:所需的選項卡標簽
TabSpec pSpec = tabHost.newTabSpec("parent");
pSpec.setIndicator("父類", this.getResources().getDrawable(R.drawable.f_root));
pSpec.setContent(R.id.txtV1);



TabSpec subSpec = tabHost.newTabSpec("sub");
subSpec.setIndicator("子類", this.getResources().getDrawable(R.drawable.f_root));
subSpec.setContent(R.id.txtV2);


//添加選項卡到TabHost中
tabHost.addTab(pSpec);
tabHost.addTab(subSpec);

}
}

Copyright © Linux教程網 All Rights Reserved