Tab组件的简单应用

来源:互联网 发布:断食减肥知乎 编辑:程序博客网 时间:2024/04/29 05:41

先来看一下主要的代码:

MianActivity.java

package com.example.tab;import android.os.Bundle;import android.app.TabActivity;import android.view.LayoutInflater;import android.view.Menu;import android.widget.TabHost;//继承TabActivitypublic class MainActivity extends TabActivity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// setContentView(R.layout.activity_main);// 获取选项卡组TabHost tabHost = getTabHost();// 引用布局文件LayoutInflater inflater = LayoutInflater.from(this);inflater.inflate(R.layout.activity_main, tabHost.getTabContentView());// 创建一个tab对象TabHost.TabSpec tab01 = tabHost.newTabSpec("tab01").setIndicator("选项一").setContent(R.id.textView1);TabHost.TabSpec tab02= tabHost.newTabSpec("tab02").setIndicator("选项二").setContent(R.id.textView2);TabHost.TabSpec tab03 = tabHost.newTabSpec("tab03").setIndicator("选项三").setContent(R.id.textView3);// 将创建好的tab放入tabHost中tabHost.addTab(tab01);tabHost.addTab(tab02);tabHost.addTab(tab03);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.activity_main, menu);return true;}}


activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <TextView        android:id="@+id/textView1"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="内容一" />        <TextView        android:id="@+id/textView2"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="内容二" />        <TextView        android:id="@+id/textView3"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="内容三" /></RelativeLayout>


运行图:

原创粉丝点击