布局文件
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="@+id/tv3"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
</TabHost>
MainActivity.java
package com.example.account;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.Toast;
@SuppressWarnings({ "deprecation" })
public class MainActivity extends TabActivity {
TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabHost = getTabHost();
//newTabSpec设置标签,跟下面响应时间对应。
//setIndicator设置页面标题。
tabHost.addTab(tabHost.newTabSpec("0").setIndicator("主页").setContent(R.id.tv1));
tabHost.addTab(tabHost.newTabSpec("1").setIndicator("流水").setContent(R.id.tv2));
tabHost.addTab(tabHost.newTabSpec("2").setIndicator("总账").setContent(R.id.tv3));
//设置背景颜色
//tabHost.setBackgroundColor(Color.argb(150, 22, 70, 150));
//设置默认界面
tabHost.setCurrentTab(0);
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
if(tabId == "0"){
Toast.makeText(MainActivity.this, "tab1", Toast.LENGTH_LONG).show();
}else if(tabId == "1"){
Toast.makeText(MainActivity.this, "tab2", Toast.LENGTH_LONG).show();
}else if(tabId == "2"){
Toast.makeText(MainActivity.this, "tab3", Toast.LENGTH_LONG).show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
遇到的问题:
1.布局
(1)TabHost的android:id必须设置为:android:id="@android:id/tabhost"
(2)TabWidget的android:id必须设置为:android:id="@android:id/tabs"
(3)FrameLayout的android:id必须设置为:android:id="@android:id/tabcontent"
2.监听事件
监听事件里的tabId == "0",这里的值必须跟tabHost.newTabSpec("0")里设置的相对应。