`

TabSpace&TabHost(选项卡).txt

 
阅读更多
TabHost:
	相当于浏览器中浏览器分页的集合
TabSpace:
	相当于浏览器中的每个分页面
	
	

	<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/tabBack"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn_back" />

    <TextView
        android:id="@+id/tabTv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/tab_tv" />

    <LinearLayout
        android:id="@+id/tab_layout3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" 
        android:background="@drawable/bg"
        >

        <Button
            android:id="@+id/tabBtn3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/tab_tv" />

        <TextView
            android:id="@+id/tabTv3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/tab_tv" />
    </LinearLayout>

</LinearLayout>





public class TabActivity extends android.app.TabActivity{
	
	private TabSpec ts1,ts2,ts3;
	private TabHost tab;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.tab_view);
		
		
		//取得TabHost
		tab = this.getTabHost();//实例(分页)菜单
		//利用LayoutInflater将布局与分页菜单一起显示
		LayoutInflater.from(this).inflate(R.layout.tab_view, tab.getTabContentView());
		
		ts1 = tab.newTabSpec("tabOne");//实例化一个分页
		ts1.setIndicator("Tab1");//设置此分页显示的标题
		ts1.setContent(R.id.tabBack);
		
		ts2 = tab.newTabSpec("tabTwo");
		//设置分页显示的标题和图标
		ts2.setIndicator("Tab2",getResources().getDrawable(R.drawable.ico));
		ts2.setContent(R.id.tabTv);
		
		ts3 = tab.newTabSpec("tabThree");
		ts3.setIndicator("Tab3");
		ts3.setContent(R.id.tab_layout3);
		//菜单中添加分页
		tab.addTab(ts1);
		tab.addTab(ts2);
		tab.addTab(ts3);
		
		tab.setOnTabChangedListener(new OnTabChangeListener() {
			@Override
			public void onTabChanged(String tabId) {
				// TODO Auto-generated method stub
				if(tabId.equals("tabOne")){ 
					Toast.makeText(TabActivity.this, "One", Toast.LENGTH_SHORT).show();
				}
					
				if(tabId.equals("tabTwo")){
					Toast.makeText(TabActivity.this, "二", Toast.LENGTH_SHORT).show();
				}
				
				if(tabId.equals("tabThree")){
					Toast.makeText(TabActivity.this, "3", Toast.LENGTH_SHORT).show();
				}
					
			}
		});
	}
}

:------------------------------------
	1.继承:android.app.TabActivity
	2.创建TabHost分页菜单对象,利用以下代码:
		TabHost tab = this.getTabHost();
		LayoutInflater.from(this).inflate(R.layout.main,tableHost.getTabContentView());
	3.声明TabSpace
		TabSpace ts = tab.newTablSpace(String tag);
		setIndicator(CharSequence label, Drawable icon)
			label:标题  icon:图标
		setContent(int arg);//设置显示内容ID
			arg:组件Id,也可以是布局Id
	4.添加至TabHost
		tab.addTab(TabSpace tabSpace);//根据添加顺序,从左往右显示
		
	5.监听分页改变事件:
		.OnTabChangedListener接口,重写OnTabChanged(String tableId)
		.tableId 是 在创建TabSpace时的值。如: TabSpace ts = tab.newTabSpace("id");

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics