您的位置:首页 > 移动开发 > Android开发

Android TabHost总结

2015-10-15 16:32 417 查看
听说现在流行用fragment Tabhost 代替TabHost,刚入门,还是从tabHost开始。

TabHost即微信底部的选项卡,直接上代码:

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TabHost 
        android:id="@+id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        
        <LinearLayout 
            android:layout_width="match_parent"    
            android:layout_height="match_parent"    
            android:orientation="vertical"    >    
            
	        <FrameLayout 
	            android:id="@android:id/tabcontent"
	            android:layout_width="match_parent"
	            android:layout_height="match_parent"
	            android:layout_weight="1" >
	            
	            <LinearLayout 
	                android:id="@+id/tab1"
	                android:layout_width="match_parent"
	                android:layout_height="match_parent"
	                android:orientation="vertical">
	                
	                <TextView 
	                    android:layout_width="wrap_content"
	                    android:layout_height="wrap_content"
	                    android:text="texttab1"
	                    />
	                
	            </LinearLayout>
	            
	            <LinearLayout 
	                android:id="@+id/tab2"
	                android:layout_width="match_parent"
	                android:layout_height="match_parent"
	                android:orientation="vertical" >
	            </LinearLayout>
	            
	            <LinearLayout 
	                android:id="@+id/tab3"
	                android:layout_width="match_parent"
	                android:layout_height="match_parent"
	                android:orientation="vertical" >
	            </LinearLayout>
	            
	            <LinearLayout 
	                android:id="@+id/tab4"
	                android:layout_width="match_parent"
	                android:layout_height="match_parent"
	                android:orientation="vertical" >
	            </LinearLayout>
	        </FrameLayout>
	        
	         <TabWidget    
                android:id="@android:id/tabs"    
                android:layout_width="match_parent"    
                android:layout_height="wrap_content" >    
            </TabWidget>  
	        
   		</LinearLayout>
   </TabHost>
    
 

</LinearLayout>


TabHost底下为FrameLayout,FrameLayout底下为LinearLayout,LinearLayout底下的每个LinearLayout就是每个选项卡的页面。
MainActivity中初始化TabHost:

public class MainActivity extends Activity {
	private TabHost tabhost;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		
		tabhost = (TabHost) findViewById(R.id.tabhost);
		tabhost.setup();    
        tabhost.addTab(tabhost.newTabSpec("tab1").setIndicator("微信")    
            .setContent(R.id.tab1));
        tabhost.addTab(tabhost.newTabSpec("tab2").setIndicator("通讯录")    
                .setContent(R.id.tab2));
        tabhost.addTab(tabhost.newTabSpec("tab3").setIndicator("发现")    
                .setContent(R.id.tab3));
            tabhost.addTab(tabhost.newTabSpec("tab4").setIndicator("我")    
                    .setContent(R.id.tab4));
	}

	@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;
	}

}


效果如下图所示



【设置标签切换触发】
tabHost.setOnTabChangedListener(new OnTabChangeListener(){    
            @Override  
            public void onTabChanged(String tabId) {  
                if (tabId.equals("tab1")) {   //第一个标签  
                }  
                if (tabId.equals("tab2")) {   //第二个标签  
                }  
                if (tabId.equals("tab3")) {   //第三个标签  
                }  
            }              
        });
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: