使用TabHost 可以在一个屏幕间进行不同版面的切换,例如android自带的拨号应用。
完成一个TabHost的步骤:
一、设计布局文件,Tabhost布局文件一般使用FrameLayout,在FrameLayout中添加每个Tab页面的视图,但必须要有id,例如:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">

<!-- 第一个Tab 对应的布局 -->
<!-- Tab内容必须用Layout布局将view包含,否则在程序中找不到View -->
<LinearLayout android:id="@+id/tab01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<ListView android:id="@+id/weather" android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>

<!-- 第二个Tab 对应的布局 -->
<LinearLayout android:id="@+id/tab02"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<TextView android:id="@+id/weath_detail"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:text="tab">
</TextView>
</LinearLayout>

<!-- 第三个Tab 对应的布局 -->
<LinearLayout android:id="@+id/tab03"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<TextView android:id="@+id/city_detail" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:text="tab">
</TextView>
</LinearLayout>
</FrameLayout>

注意:每个Tab页面都要有自己的layout,负责在代码中通过Id无法找到相对应的视图
二、创建展示TabHost的Activity。
1、我们可以直接继承TabActivity,再通过getTabHost()方法得到TabHost对象。例如:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
myTabHost = this.getTabHost();
//绑定布局文件
LayoutInflater.from(this).inflate(R.layout.tab_host,
myTabHost.getTabContentView(),
true);

initTabHost();
setTitle(weather.getCityName());
//设定显示内容
setContentView(myTabHost);
}

2,为TabHost添加要显示的tab,一个tab就是相应的一个选项卡。例如:
//添加tab
myTabHost.addTab(myTabHost.newTabSpec(TAB_1) //tab的Tag
.setIndicator("城市天气") //tab的标题
.setContent(R.id.tab01)); //tab的显示内容
myTabHost.addTab(myTabHost.newTabSpec(TAB_2)
.setIndicator("天气详情")
.setContent(R.id.tab02));
myTabHost.addTab(myTabHost.newTabSpec(TAB_3)
.setIndicator("城市介绍")
.setContent(R.id.tab03));
3,为Tabhost添加选项卡改变监听,在选项卡改变时做相应处理。例如:
//添加OnTabChangedListener监听,此监听为选项卡改变监听
myTabHost.setOnTabChangedListener(this);
监听处理方法
public void onTabChanged(String tabId)
{
if (tabId.equals(TAB_1))
{
//初始化标签1
initTab1();
}
else if (tabId.equals(TAB_2))
{
//初始化标签2
initTab2();
}
else if (tabId.equals(TAB_3))
{
//初始化标签3
initTab3();
}

}

4,修改Tab显示内容
private void initTab2()
{
//得到tab内容的视图
TextView weathDetailText = (TextView)findViewById(R.id.weath_detail);
weathDetailText.setText(weather.getLiveWeather());
}

更多相关文章

  1. Android-MVVM架构-Data Binding的使用
  2. Android(安卓)稀奇古怪的问题
  3. Android(安卓)ExpandableListView简单使用(一)之仿QQ添加分组功能
  4. Android(安卓)UI—仿微信底部导航栏布局
  5. Android(安卓)DataBinding的使用
  6. Android(安卓)学习笔记之界面布局
  7. day01
  8. 【转】android中onMeasure初看,深入理解布局之一!
  9. Android的百度地图开发BaiduMapApiDemo

随机推荐

  1. 节操视频播放器
  2. Android中如何自定义view
  3. android 倒计时
  4. Android(安卓)EditText样式
  5. Android CheckBox控件使用OnClickListene
  6. 《android的事件分发机制》
  7. Android getSystemService
  8. Momo自定义DialogFragment
  9. Android Studio NDK Opencv fatal error:
  10. Android学习笔记_20_访问应用权限汇总