Android 在fragment中使用tabhost

在fragment中使用tabhost关键点有两个:

(1),xml文件,TabWidget的id和放置tab对应内容的容器的id。

(2),tabHost和设置的方式,因为类继承fragment类(或子类),而不是Activity,更不是TabActivity。

主要代码:

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/academic"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <TabHost 
        android:id="@+id/tabhost"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
            <!-- TabWidget 的 id 必须是这个‘@android:id/tabs’,否则会抛出错误 -->
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:orientation="horizontal"
                 />
            <!-- tab对应内容的容器,id必须是‘@android:id/tabcontent’,否则会抛出错误 -->
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_alignTop="@android:id/tabs"
                >
                
                <fragment
                    android:id="@+id/frag1"
                    android:name="com.herald.ezherald.academic.Fragment1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
                
                <fragment
                    android:id="@+id/frag2"
                    android:name="com.herald.ezherald.academic.Fragment2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
                
            </FrameLayout>
        
        </RelativeLayout>
        
    </TabHost>
    
</LinearLayout>

Java代码:

Javapublic class AcademicFragment extends SherlockFragment implements OnItemClickListener, TabHost.TabContentFactory, TabHost.OnTabChangeListener {
    String text = null;
    LinearLayout layout;
    TabHost tabHost;
    
    public AcademicFragment()
    {
        text = "Default";
    }

    /* (non-Javadoc)
     * @see android.support.v4.app.Fragment#setArguments(android.os.Bundle)
     */
    @Override
    public void setArguments(Bundle args) {
        // TODO Auto-generated method stub
        super.setArguments(args);
        text = args.getString("text");
    }


    /* (non-Javadoc)
     * @see android.support.v4.app.Fragment#onCreate(android.os.Bundle)
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    /* (non-Javadoc)
     * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
     */
    // 这里是最主要的部分
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        layout = (LinearLayout) inflater.inflate(R.layout.academic_activity_main, null);
        tabHost =(TabHost) layout.findViewById(R.id.tabhost);
        tabHost.setup();
        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1").setContent(R.id.frag1));
        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("tab2").setContent(R.id.frag2));
        
        tabHost.setCurrentTab(0); 
        tabHost.setOnTabChangedListener(this);
        return layout;
    }
    
     @Override 
     public void onTabChanged(String tabId) { 
            // TODO Auto-generated method stub 
      }

    @Override
    public View createTabContent(String tag) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        
    }
    
    
}

代码解释:

Java代码里面涉及到了两个fragment,就是普通定义的fragment,没什么特别的。

onCreateView() 方法里是 TabHost 的主要部分,获取TabHost的方法是先用 LayoutInflater抽象类获取布局对象,然后用布局对象获取TabHost,因为不是继承Activity,所以不能直接用getViewById()。

别忘了 tabHost.setup()。

可能遇到的问题:

Q:tab布局出来了,但是对应的内容不显示

A:原因很可能是xml布局产生的的问题。尤其注意TabWidget和tab对应内容的容器(本例是FrameLayout)。

原文地址:https://www.cnblogs.com/wolf-bing/p/3151452.html