android:TabHost总结

Tabs:标签、选项

在android布局中,Tabs具有良好的用户体验

Tabs in the action bar make it easy to explore and switch between different views or functional aspects of your app, or to browse categorized data sets.

然后总结一下TabHost

public class

TabHost

extends FrameLayout
implements ViewTreeObserver.OnTouchModeChangeListener
java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.FrameLayout
         ↳ android.widget.TabHost

继承自FrameLayout,并实现了触摸监听接口

Class Overview

 分页式窗口视图的一种容器,包含两种子元素:标签(TabWidget)和对应的内容页(FrameLayout)。TabHost将这些元素统一起来进行控制。

TabHost的两种实现方法:

第一种:继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost。

        myTabhost=this.getTabHost();  
        //get Tabhost  
        LayoutInflater.from(this).inflate(R.layout.main, myTabhost.getTabContentView(), true);  
        myTabhost.setBackgroundColor(Color.argb(150, 22, 70, 150));  
          
        myTabhost  
                .addTab(myTabhost.newTabSpec("One")// make a new Tab  
                        // set the Title and Icon  
                        .setIndicator("A")
                        .setContent(R.id.widget_layout_Blue));  
        // set the layout  

第二种:不继承TabActivity,在布局文件中定义TabHost

  TabWidget的id必须是@android:id/tabs

  FrameLayout的id必须是@android:id/tabcontent

  TabHost的id可以自定义

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    android:id="@+id/hometabs"  
    android:orientation="vertical"  
    android:layout_width="fill_parent"    
    android:layout_height="fill_parent">   
    <TabHost android:id="@+id/tabhost"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content">  
        <LinearLayout  
            android:orientation="vertical"  
            android:layout_width="fill_parent"  
            android:layout_height="fill_parent">  
              
            <TabWidget android:id="@android:id/tabs"   
              android:orientation="horizontal"  
              android:layout_width="wrap_content"  
              android:layout_height="wrap_content">  
            </TabWidget>  
           
             <FrameLayout android:id="@android:id/tabcontent"  
                  android:layout_width="wrap_content"  
                  android:layout_height="wrap_content">  
                    <TextView android:id="@+id/view1"  
                        android:layout_width="fill_parent"  
                        android:layout_height="fill_parent"/>  
                    <TextView android:id="@+id/view2"  
                        android:layout_width="fill_parent"  
                        android:layout_height="fill_parent"/>  
                    <TextView android:id="@+id/view3"  
                        android:layout_width="fill_parent"  
                        android:layout_height="fill_parent"/>  
             </FrameLayout>  
           
         </LinearLayout>  
    </TabHost>  
</LinearLayout>  

实现代码:

protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.hometabs);  
          
        TabHost tabHost = (TabHost) findViewById(R.id.tabhost);  
        tabHost.setup();  
        TabWidget tabWidget = tabHost.getTabWidget();  
          
        tabHost.addTab(tabHost.newTabSpec("tab1")  
                .setIndicator("tab1", getResources().getDrawable(R.drawable.mumule))  
                .setContent(R.id.view1));  
          
        tabHost.addTab(tabHost.newTabSpec("tab3")  
                .setIndicator("tab3")  
                .setContent(R.id.view3));  
          
        tabHost.addTab(tabHost.newTabSpec("tab2")  
                .setIndicator("tab2")  
                .setContent(R.id.view2));    
        
    }  

  

 

原文地址:https://www.cnblogs.com/skiz/p/2734321.html