版权声明:本文为博主原创文章,未经博主同意不得转载。

https://blog.csdn.net/he90227/article/details/24474197

直接上代码 -- 基于Android4.4

package com.example.viewdemo1;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost; //新版本号中TabActivity过时了,使用一下的方式
public class Tab1Activity extends Activity{ private TabHost myTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab1);
myTabHost = (TabHost) findViewById(android.R.id.tabhost);
myTabHost.setup();//实例化了tabWidget和tabContent
myTabHost.setBackgroundColor(Color.argb(150, 22, 70, 150)); myTabHost.addTab(myTabHost.newTabSpec("腾讯").setIndicator("腾讯",getResources().getDrawable(R.drawable.qq)).setContent(R.id.tab1));
myTabHost.addTab(myTabHost.newTabSpec("华为").setIndicator("华为",getResources().getDrawable(R.drawable.huawei)).setContent(R.id.tab2));
myTabHost.addTab(myTabHost.newTabSpec("百度").setIndicator("百度",getResources().getDrawable(R.drawable.baidu)).setContent(R.id.tab3));
} /*
//Android4之前的方式。4之后TabActivity已经过期了
public class Tab1Activity extends android.app.TabActivity{ private TabHost myTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myTabHost = this.getTabHost(); LayoutInflater.from(this).inflate(R.layout.activity_tab1, myTabHost.getTabContentView(),true);
myTabHost.setBackgroundColor(Color.argb(150, 22, 70, 150)); myTabHost.addTab(myTabHost.newTabSpec("腾讯").setIndicator("腾讯",getResources().getDrawable(R.drawable.qq)).setContent(R.id.tab1));
myTabHost.addTab(myTabHost.newTabSpec("华为").setIndicator("华为",getResources().getDrawable(R.drawable.huawei)).setContent(R.id.tab2));
myTabHost.addTab(myTabHost.newTabSpec("百度").setIndicator("百度",getResources().getDrawable(R.drawable.baidu)).setContent(R.id.tab3));
}*/ @Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
return super.onCreateOptionsMenu(menu);
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
return super.onOptionsItemSelected(item);
} }

布局文件

<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"> <TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" > <LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" > <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一个Tab选项卡" /> </LinearLayout> <LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二个Tab选项卡" />
</LinearLayout> <LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第三个Tab选项卡" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost> </LinearLayout>

05-11 13:38