首先看布局文件activity_main.xml
<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"
android:orientation="vertical" >
<TextView
android:id="@+id/content1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="乘风破浪会有时,直挂云帆济沧海。
别让怯弱否定自己,别让惫懒误了青春。一个人不奋斗不能有所成就,一个国家不奋斗不能立足世界,一个民族不奋斗不能兴盛强大。 " />
<TextView
android:id="@+id/content2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="带奋斗一起飞翔,因为有了它,让我拥有理智之思;我才使过去的失误不再重演到今天的影片里;我才能使过去的成功在人生中继续升华;
" />
<TextView
android:id="@+id/content3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="If you would know the value of money, go and try to borrow some. " />
</LinearLayout>
然后是 编写MainActivity并进行相应的事件处理
public class MainActivity extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TabHost tabHost = getTabHost();
LayoutInflater inflater = LayoutInflater.from(this);
inflater.inflate(R.layout.activity_main, tabHost.getTabContentView());
TabHost.TabSpec tab01 = tabHost.newTabSpec("tab01")
.setIndicator("夏沫之殇", getResources().getDrawable(R.drawable.image1))
.setContent(R.id.content1);
TabHost.TabSpec tab02 = tabHost.newTabSpec("tab02")
.setIndicator("泡沫般愛情",getResources().getDrawable(R.drawable.image2))
.setContent(R.id.content2);
TabHost.TabSpec tab03 = tabHost.newTabSpec("tab03")
.setIndicator("思念已成灰", getResources().getDrawable(R.drawable.image3))
.setContent(R.id.content3);
tabHost.addTab(tab01);
tabHost.addTab(tab02);
tabHost.addTab(tab03);
tabHost.setCurrentTab(0);
tabHost.setBackgroundResource(R.drawable.bg);
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
//红色部分是获取.setIndicator()方法的标签,因为没有找到相应的get方法,可能有别的调用方法,待日后发现再做补充
TabWidget widget=tabHost.getTabWidget();
final TextView textView=(TextView) widget.getChildAt(tabHost.getCurrentTab()).findViewById(android.R.id.title);
Dialog dialog = new AlertDialog.Builder(MainActivity.this)
.setTitle("提示")
.setMessage("当前是:"+textView.getText().toString()+"的心情面板")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
}).create();
dialog.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;
}
}
后是标题栏的属性设置,可参考 日志《 》,方法是一样的。
效果图: