如何在Android中的片段中引入Tab布局

时间:2012-11-19 18:51:13

标签: android android-layout android-fragments

我的活动中有两个片段。这很好。现在在我的一个片段中,我需要两个标签内容。在每个标签中,我需要给视图充气。我怎么能在oncreateview方法中做到这一点?有可能吗?

这是我的XML布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="match_parent"
android:background="@drawable/texture_background"
android:baselineAligned="false"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="#000" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_marginBottom="10dp"
        android:padding="10dp"
        android:text="header"
        android:textColor="#fff"
        android:textSize="25dp" >
    </TextView>
</LinearLayout>
<TabHost
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:visibility="gone" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>
</TabHost>
</LinearLayout>

这是我片段中的oncreateview方法。

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
  View view = inflater.inflate(R.layout.saved_deals_fragment, container,false);
  TabHost tabs=(TabHost)view.findViewById(android.R.id.tabhost);
  tabs.setup();
  TabHost.TabSpec tab = tabs.newTabSpec("my tab content");
  tab.setIndicator("my tab content");
  tabs.addTab(tab);
  return view;
}

这是我的错误:

    11-19 21:29:32.666: E/AndroidRuntime(28686): FATAL EXCEPTION: main
11-19 21:29:32.666: E/AndroidRuntime(28686): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.WrapperActivity}:   java.lang.IllegalArgumentException: you must specify a way to create the tab content
11-19 21:29:32.666: E/AndroidRuntime(28686):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
11-19 21:29:32.666: E/AndroidRuntime(28686):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11-19 21:29:32.666: E/AndroidRuntime(28686):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-19 21:29:32.666: E/AndroidRuntime(28686):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
11-19 21:29:32.666: E/AndroidRuntime(28686):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-19 21:29:32.666: E/AndroidRuntime(28686):    at android.os.Looper.loop(Looper.java:137)
11-19 21:29:32.666: E/AndroidRuntime(28686):    at android.app.ActivityThread.main(ActivityThread.java:5039)
11-19 21:29:32.666: E/AndroidRuntime(28686):    at java.lang.reflect.Method.invokeNative(Native Method)
11-19 21:29:32.666: E/AndroidRuntime(28686):    at java.lang.reflect.Method.invoke(Method.java:511)
11-19 21:29:32.666: E/AndroidRuntime(28686):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-19 21:29:32.666: E/AndroidRuntime(28686):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-19 21:29:32.666: E/AndroidRuntime(28686):    at dalvik.system.NativeStart.main(Native Method)
11-19 21:29:32.666: E/AndroidRuntime(28686): Caused by: java.lang.IllegalArgumentException: you must specify a way to create the tab content
11-19 21:29:32.666: E/AndroidRuntime(28686):    at android.widget.TabHost.addTab(TabHost.java:225)
11-19 21:29:32.666: E/AndroidRuntime(28686):    at com.example.fragments.SavedFragment.onCreateView(SavedDealsFragment.java:120)
11-19 21:29:32.666: E/AndroidRuntime(28686):    at android.app.Fragment.performCreateView(Fragment.java:1695)
11-19 21:29:32.666: E/AndroidRuntime(28686):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
11-19 21:29:32.666: E/AndroidRuntime(28686):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
11-19 21:29:32.666: E/AndroidRuntime(28686):    at android.app.BackStackRecord.run(BackStackRecord.java:682)
11-19 21:29:32.666: E/AndroidRuntime(28686):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
11-19 21:29:32.666: E/AndroidRuntime(28686):    at android.app.Activity.performStart(Activity.java:5113)
11-19 21:29:32.666: E/AndroidRuntime(28686):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)

我是否需要提供任何其他详细信息?

1 个答案:

答案 0 :(得分:2)

您不提供标签的内容。您可以使用TabContentFactory来构建如下内容:

TabHost.TabSpec tab = tabs.newTabSpec("tab1");
tab.setIndicator("my tab content");
tab.setContent(new TabHost.TabContentFactory() {

            @Override
            public View createTabContent(String tag) {
                if (tag.equals("tab1")) {
                    // create/inflate the view for the tab 1
                    return view;
                } else {
                    // the same as tab 1 for the other tabs if you have any             
            }
        });
tabs.addTab(tab);