Tabhost 运用详解

来源:互联网 发布:中央12网络电视台直播 编辑:程序博客网 时间:2024/05/22 12:58



------------------------------------------Tabhost布局

<?xml version="1.0" encoding="utf-8"?>


<TabHost android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"

>
<LinearLayout android:id="@+id/LinearLayout01"
android:orientation="vertical" android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TabWidget android:id="@android:id/tabs"
android:layout_height="wrap_content" android:layout_width="fill_parent"></TabWidget>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_height="fill_parent" android:layout_width="fill_parent"></FrameLayout>
</LinearLayout>


</TabHost>

--------------------------------------解释:
<TabWidget android:id="@android:id/tabs"
android:layout_height="wrap_content" android:layout_width="fill_parent"></TabWidget>


----android:layout_height="wrap_content" 需要是wrap_content属性.


--------------------------------.JAVA

package com.android.tab;


import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;


public class TabBarExample extends TabActivity {


private int myMenuRes[] = {
R.drawable.tab1,
R.drawable.tab2,
R.drawable.tab3,
R.drawable.tab4,
R.drawable.tab5
};


TabHost tabHost;
TabSpec firstTabSpec;
TabSpec secondTabSpec;
TabSpec threeTabSpec;
TabSpec fourTabSpec;
TabSpec fiveTabSpec;
TabSpec six;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);


setTitle("最後一堂課");


/* TabHost will have Tabs */
tabHost = (TabHost) findViewById(android.R.id.tabhost);
// tabHost.setBackgroundResource(R.drawable.nav_background);


/*
* TabSpec used to create a new tab. By using TabSpec only we can able
* to setContent to the tab. By using TabSpec setIndicator() we can set
* name to tab.
*/


/* tid1 is firstTabSpec Id. Its used to access outside. */
firstTabSpec = tabHost.newTabSpec("tid1");
secondTabSpec = tabHost.newTabSpec("tid2");
threeTabSpec = tabHost.newTabSpec("tid3");
fourTabSpec = tabHost.newTabSpec("tid4");
fiveTabSpec = tabHost.newTabSpec("tid5");
six=tabHost.newTabSpec("最後一次戰鬥");


/* TabSpec setIndicator() is used to set name for the tab. */
/* TabSpec setContent() is used to set content for a particular tab. */
six.setIndicator("finally",getResources().getDrawable(R.drawable.maket));
firstTabSpec.setIndicator("Latest", getResources().getDrawable(
myMenuRes[0]));
secondTabSpec.setIndicator("Topics", getResources().getDrawable(
myMenuRes[1]));
threeTabSpec.setIndicator("Video", getResources().getDrawable(
myMenuRes[2]));
fourTabSpec.setIndicator("Podcast", getResources().getDrawable(
myMenuRes[3]));
fiveTabSpec.setIndicator("Gallery", getResources().getDrawable(
myMenuRes[4]));


six.setContent(new Intent(this,FirstTab.class));
firstTabSpec.setContent(new Intent(this, FirstTab.class));

secondTabSpec.setContent(new Intent(this, SecondTab.class));
threeTabSpec.setContent(new Intent(this, FirstTab.class));
fourTabSpec.setContent(new Intent(this, SecondTab.class));
fiveTabSpec.setContent(new Intent(this, SecondTab.class));


/* Add tabSpec to the TabHost to display. */
tabHost.addTab(firstTabSpec);
tabHost.addTab(secondTabSpec);
tabHost.addTab(threeTabSpec);
tabHost.addTab(fourTabSpec);
tabHost.addTab(fiveTabSpec);
tabHost.addTab(six);
}
}

原创粉丝点击