Android入门之TabHost与TabWidget实例解析

所属分类: 软件编程 / Android 阅读数: 1427
收藏 0 赞 0 分享

本文实例介绍的是Android的Tab控件,Tab控件可以达到分页的效果,让一个屏幕的内容尽量丰富,当然也会增加开发的复杂程度,在有必要的时候再使用。Android的Tab控件使用起来有点奇怪,必须包含和按照以下的顺序:

TabHost控件->TabWidget(必须命名为tabs)->FrameLayout(必须命名为tabcontent)。

先来贴出本例运行的截图:

main.xml的源码如下:

<?xml version="1.0" encoding="utf-8"?>
<TabHost android:layout_width="fill_parent"
  android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/TabHost1">
  <TabWidget android:id="@android:id/tabs"
    android:layout_height="wrap_content" android:layout_width="fill_parent">
</TabWidget>
  <FrameLayout android:id="@android:id/tabcontent"
    android:paddingTop="65px" android:layout_width="fill_parent" android:layout_height="fill_parent">
    <LinearLayout android:layout_height="wrap_content" android:id="@+id/Tab1" android:orientation="vertical" android:layout_width="fill_parent">
      <EditText android:layout_height="wrap_content" android:id="@+id/edtTab1" android:layout_width="fill_parent"></EditText>
      <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnTab1" android:text="Tab1"></Button>
    </LinearLayout>
    <LinearLayout android:layout_height="wrap_content" android:id="@+id/Tab2" android:layout_width="fill_parent" android:orientation="horizontal">
      <EditText android:layout_height="wrap_content" android:id="@+id/edtTab2" android:layout_width="wrap_content" android:layout_weight="300"></EditText>
      <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnTab2" android:text="Tab2"></Button></LinearLayout>
  </FrameLayout>
</TabHost>

java程序源码如下:

package com.testTab;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class testTab extends TabActivity {//基于TabActivity构建
 
 Button btnTab1,btnTab2;
 EditText edtTab1,edtTab2;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    TabHost tabs = getTabHost();
    //设置Tab1
    TabSpec tab1 = tabs.newTabSpec("tab1");
    tab1.setIndicator("tab1");   // 设置tab1的名称
    tab1.setContent(R.id.Tab1);  // 关联控件
    tabs.addTab(tab1);        // 添加tab1
    
    btnTab1=(Button)this.findViewById(R.id.btnTab1);
    edtTab1=(EditText)this.findViewById(R.id.edtTab1);
    btnTab1.setOnClickListener(new ClickEvent());
    
    //设置Tab2
    TabSpec tab2 = tabs.newTabSpec("tab2");
    tab2.setIndicator("tab2");   
    tab2.setContent(R.id.Tab2);  
    tabs.addTab(tab2);        
    
    btnTab2=(Button)this.findViewById(R.id.btnTab2);
    edtTab2=(EditText)this.findViewById(R.id.edtTab2);
    btnTab2.setOnClickListener(new ClickEvent());
    
    tabs.setCurrentTab(0);
  }
  
  class ClickEvent implements View.OnClickListener {
 @Override
 public void onClick(View v) {
  if(v==btnTab1)
  {
  edtTab1.setText("tab1");
  }
  else if(v==btnTab2)
  {
  edtTab2.setText("tab2");
  }
 }
  
  }
}
更多精彩内容其他人还在看

Android异常 java.lang.IllegalStateException解决方法

这篇文章主要介绍了Android异常 java.lang.IllegalStateException解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Split()字符串分割特殊用法案例详解

本文通过案例的形式给大家详细介绍了android中split()字符串分割特殊用法的知识,非常不错具有参考借鉴价值,感兴趣的朋友参考下
收藏 0 赞 0 分享

Android仿新浪微博启动界面或登陆界面(1)

这篇文章主要为大家详细介绍了Android仿新浪微博启动界面或登陆界面的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android仿新浪微博oauth2.0授权界面实现代码(2)

这篇文章主要为大家详细介绍了Android仿新浪微博oauth2.0授权界面实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android开发中使用sqlite实现新闻收藏和取消收藏的功能

本篇文章主要介绍了sqlite实现新闻收藏和取消收藏功能,主要涉及到oracle数据库方面的内容,对于Android开发sqlite实现收藏和取消功能感兴趣的朋友可以参考下本文
收藏 0 赞 0 分享

Android仿新浪微博分页管理界面(3)

这篇文章主要为大家详细介绍了Android仿新浪微博分页管理界面,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android UI自定义ListView实现下拉刷新和加载更多效果

这篇文章主要介绍了Android UI自定义ListView实现下拉刷新和加载更多效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android—基于微信开放平台v3SDK开发(微信支付填坑)

这篇文章主要介绍了Android—基于微信开放平台v3SDK开发(微信支付填坑),具有一定的参考价值,有需要的可以了解一下。
收藏 0 赞 0 分享

Android仿新浪微博自定义ListView下拉刷新(4)

这篇文章主要为大家详细介绍了Android仿新浪微博自定义ListView下拉刷新,重点介绍了Adapter的详细代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android控件之使用ListView实现时间轴效果

这篇文章主要介绍了Android基础控件之使用ListView实现时间轴效果的相关资料,本文是以查看物流信息为例,给大家介绍了listview时间轴的实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多