tab切换的简单实现

1.main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/content1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="内容1" />

    <TextView
        android:id="@+id/content2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="内容2" />

    <TextView
        android:id="@+id/content3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="内容3" />
</LinearLayout

 

2.MainActivity.java

public class MainActivity extends TabActivity {

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  // 第二步:获取选项卡组
  TabHost tabHost = getTabHost();
  // 第三步:引用布局文件
  LayoutInflater inflater = LayoutInflater.from(this);
  inflater.inflate(R.layout.activity_main, tabHost.getTabContentView());

  Resources resources = getResources();

  // 第四步:创建一个Tab对象
  TabHost.TabSpec tab01 = tabHost.newTabSpec("tab01")
    .setIndicator("选项1", resources.getDrawable(R.drawable.p1))
    .setContent(R.id.content1);

  TabHost.TabSpec tab02 = tabHost.newTabSpec("tab02")
    .setIndicator("选项2", resources.getDrawable(R.drawable.p2))
    .setContent(R.id.content2);
  TabHost.TabSpec tab03 = tabHost.newTabSpec("tab03")
    .setIndicator("选项3", resources.getDrawable(R.drawable.p3))
    .setContent(R.id.content3);
  // 第五步:将创建好的Tab对象放入到tabHost中
  
  tabHost.addTab(tab01);
  tabHost.addTab(tab02);
  tabHost.addTab(tab03);
  tabHost.setCurrentTab(1);

 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值