选项卡的使用方法一(Android学习随笔十二)

选项卡的使用方法一(Android学习随笔十二)

1、继承TabActivity;

2、用getTabHost()方法获取TabHost;

3、各Tab内容在布局文件中定义。

java代码:

package yc.demo; import android.app.TabActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.widget.TabHost; import android.widget.TabHost.TabSpec; public class TabhostDemo2Activity extends TabActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //获取到TabHost TabHost tabHost = this.getTabHost(); //把我们的布局文件添加到tabHost的FrameLayout下面 LayoutInflater.from(this).inflate(R.layout.demo2, tabHost.getTabContentView(), true); //获取一个新的TabHost.TabSpec,并关联到当前tabHost //参数:所需的选项卡标签 TabSpec pSpes = tabHost.newTabSpec("parent"); pSpes.setIndicator("父类", this.getResources().getDrawable(R.drawable.msg_icon)); pSpes.setContent(R.id.tv1); TabSpec subSpec = tabHost.newTabSpec("sub"); subSpec.setIndicator("子类",this.getResources().getDrawable(R.drawable.at_icon)); subSpec.setContent(R.id.tv2); //向tabHost中添加选项卡 tabHost.addTab(subSpec); tabHost.addTab(pSpes); } }


xml布局文件:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/tab1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:id="@+id/tv1" android:layout_height="fill_parent" android:layout_width="fill_parent" android:text="parent"/> </LinearLayout> <LinearLayout android:id="@+id/tab2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:id="@+id/tv2" android:layout_height="fill_parent" android:layout_width="fill_parent" android:text="sub"/> </LinearLayout> </LinearLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值