主类:
package com.example.day15title;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
import android.widget.Toast;
public class MainActivity extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);这个一定要注释掉
TabHost tabHost = getTabHost();//获取TabHost
//获取视图
LayoutInflater.from(this).inflate(R.layout.activity_main, tabHost.getTabContentView(), true);
//通过布局的tab1 newTabSpec 设置标题 设置图片
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("美照1").setContent(R.id.tab01));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("美照2").setContent(R.id.tab02));
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("美照3").setContent(R.id.tab03));
tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
Toast.makeText(MainActivity.this, "你最美你最美你最美", Toast.LENGTH_SHORT).show();
}
});
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/tab01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/a" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/tab02"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/b" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/tab03"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/c" />
</LinearLayout>
</FrameLayout>