android tabhost的使用方法,android TabHost的基本使用

1.概念

盛放Tab的容器就是TabHost。TabHost的实现有两种方式:

第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost。各个Tab中的内容在布局文件中定义就行了。

第二种方式,不继承TabActivity,在布局文件中定义TabHost即可,但是TabWidget的id必须是@android:id/tabs,FrameLayout的id必须是@android:id/tabcontent。

2.案例

1)继承TabActivity

res/layout/main.xml

HelloTabHost.java

public class HelloTabHost extends TabActivity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//调用TabActivity的getTabHost()方法获取TabHost对象

TabHost tabHost = getTabHost();

//设置使用TabHost布局

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

//添加第一个标签页

tabHost.addTab(tabHost.newTabSpec("tab01").setIndicator("已接电话").setContent(R.id.tab01));

//添加第二个标签页,并在其标签上添加一个图片

tabHost.addTab(tabHost.newTabSpec("tab02").setIndicator("未接电话",getResources().getDrawable(R.drawable.icon)).setContent(R.id.tab02));

//添加第三个标签页

tabHost.addTab(tabHost.newTabSpec("tab03").setIndicator("已拨电话").setContent(R.id.tab03));

}

}运行效果图:

0818b9ca8b590ca3270a3433284dd417.png

2)不继承TabActivity

继承普通Activity,标签id必须为tabs、标签id必须为tabcontent.这个方式在通过findViewById获得TabHost之后,必须要调用setup方法。

main.xml代码

android:layout_height="wrap_content" />

Activity中代码:

public class TabHostTest extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

// 获取TabHost对象

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

// 如果没有继承TabActivity时,通过该种方法加载启动tabHost

tabHost.setup();

tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("第一个标签",

getResources().getDrawable(R.drawable.icon)).setContent(

R.id.view1));

tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("第三个标签")

.setContent(R.id.view3));

tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("第二个标签")

.setContent(R.id.view2));

}

}

效果图:

0818b9ca8b590ca3270a3433284dd417.png

自定义TabHost的 TabWidget:

define.xml:

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="@drawable/bg"

android:orientation="vertical"

android:gravity="center"

>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/ic_launcher"

/>

android:id="@+id/tv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

/>

在Activity中的使用:

private View getView(String name){

LayoutInflater inflater = getLayoutInflater();

View v = inflater.inflate(R.layout.define, null);

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

tv.setText(name);

return v;

}

在tabHost中的设置头部的TabWidget:

TabSpec tab1 = tabHost.newTabSpec("one");

tab1.setIndicator(getView("首页")); //设置自定义的TabWidget

效果图:

0818b9ca8b590ca3270a3433284dd417.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值