TabHost的使用

首先看布局文件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;
	}


}


后是标题栏的属性设置,可参考 日志《ListView应用之模拟新浪微博界面》,方法是一样的。

效果图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值