您的位置:首页 > 移动开发 > Android开发

android TabHost使用方法

2011-11-14 20:52 411 查看
android 实现tab视图有2种方法,一种是在布局页面中定义<tabhost>标签,另一种就是继承tabactivity。如果页面比较复杂的话XML文件会写得比较庞大,用第二种方式XML页面相对要简洁得多。通过XML文件可以很方便的进行页面布局但,比如可以把 tab 的位置放在屏幕的下方。

先看继承方法:

XML文件:

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/linearLayout1"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

<TextView

android:id="@+id/textView1"

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

<FrameLayout

android:layout_width="match_parent"

android:layout_height="400dp" >

<LinearLayout

android:id="@+id/linearLayout2"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

<TextView

android:id="@+id/tab1"

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

<TextView

android:id="@+id/tab2"

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

</LinearLayout>

</FrameLayout>

</LinearLayout>

Java代码:

public class TabtestActivity extends TabActivity {

TabHost mTabHost;

TextView tv,tv1,tv2,tv3;

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

//取得TabHost对象

mTabHost = this.getTabHost();

LayoutInflater.from(this).inflate(R.layout.main,mTabHost.getTabContentView(), true);

tv = (TextView)findViewById(R.id.textView1);

tv.setText("Tab测试!");

tv1 = (TextView)findViewById(R.id.tab1);

tv2 = (TextView)findViewById(R.id.tab2);

//新建一个newTabSpec(newTabSpec)

//设置其标签和图标(setIndicator)

//设置内容(setContent)

mTabHost.addTab(mTabHost.newTabSpec("tab_test1")

.setIndicator("TAB 1")

.setContent(R.id.linearLayout2));

mTabHost.addTab(mTabHost.newTabSpec("tab_test2")

.setIndicator("TAB 2")

.setContent(R.id.linearLayout2));

mTabHost.addTab(mTabHost.newTabSpec("tab_test3")

.setIndicator("TAB 3")

.setContent(R.id.linearLayout2));

//设置TabHost的背景颜色

mTabHost.setBackgroundColor(Color.argb(150, 22, 70, 150));

//设置TabHost的背景图片资源

mTabHost.setBackgroundResource(R.drawable.bg0);

//设置当前显示哪一个标签

mTabHost.setCurrentTab(0);

//标签切换事件处理,setOnTabChangedListener

mTabHost.setOnTabChangedListener(new OnTabChangeListener()

{

public void onTabChanged(String tabId)

{

tv1.setText("Tab测试"+ tabId +"!");

tv2.setText("Tab测试"+ tabId +"!");

}

});

setContentView(mTabHost);

}

}

继承时先让自己的类继承TabActivity,然后通过调用getTabHost()方法得到tabhost对象,然后把自己写好的数据展示的布局文件加载到tabhost中,就可以实现了。最后是通过调用addTab()方法添加标签的相关属性(如:标签名称,标签图片,标签内容布局)。

XML布局方法:

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="match_parent"

android:layout_height="match_parent" >

<LinearLayout

android:id="@+id/linearLayout1"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

<TextView

android:id="@+id/textView1"

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

<FrameLayout

android:id="@android:id/tabcontent"

android:layout_width="match_parent"

android:layout_height="400dp" >

<LinearLayout

android:id="@+id/linearLayout2"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

<TextView

android:id="@+id/tab1"

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

<TextView

android:id="@+id/tab2"

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

</LinearLayout>

</FrameLayout>

<TabWidget

android:id="@android:id/tabs"

android:layout_width="match_parent"

android:layout_height="wrap_content" >

</TabWidget>

</LinearLayout>

</TabHost>

Java代码:

public class TabtestActivity extends Activity {

TabHost mTabHost;

TextView tv,tv1,tv2,tv3;

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

//取得TabHost对象

mTabHost = (TabHost)findViewById(R.id.tabhost);

mTabHost.setup();


tv = (TextView)findViewById(R.id.textView1);

tv.setText("Tab测试!");

tv1 = (TextView)findViewById(R.id.tab1);

tv2 = (TextView)findViewById(R.id.tab2);

//新建一个newTabSpec(newTabSpec)

//设置其标签和图标(setIndicator)

//设置内容(setContent)

mTabHost.addTab(mTabHost.newTabSpec("tab_test1")

.setIndicator("TAB 1")

.setContent(R.id.linearLayout2));

mTabHost.addTab(mTabHost.newTabSpec("tab_test2")

.setIndicator("TAB 2")

.setContent(R.id.linearLayout2));

mTabHost.addTab(mTabHost.newTabSpec("tab_test3")

.setIndicator("TAB 3")

.setContent(R.id.linearLayout2));

//设置TabHost的背景颜色

mTabHost.setBackgroundColor(Color.argb(150, 22, 70, 150));

//设置TabHost的背景图片资源

mTabHost.setBackgroundResource(R.drawable.bg0);

//设置当前显示哪一个标签

mTabHost.setCurrentTab(0);

//标签切换事件处理,setOnTabChangedListener

mTabHost.setOnTabChangedListener(new OnTabChangeListener()

{

public void onTabChanged(String tabId)

{

tv1.setText("Tab测试"+ tabId +"!");

tv2.setText("Tab测试"+ tabId +"!");

}

});

}

}

而如果通过XML文件配置tabHost则需要注意的是,根布局必须是 TabHost,并且只有TabHost的d可以是自定义的,framelayout、tabwidge标签的id都必须引用系统的id@android:id/tabcontent,@android:id/tabs),不然会报异常。在程序中使用findViewById()加载tabhost,然后调用tabhost.setup()方法初始化tabhost,后面的步骤则和上面一样。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: