android TabHost使用方法

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,后面的步骤则和上面一样。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值