在DialogFragment中放置选项卡

时间:2015-02-15 02:59:10

标签: android android-tabhost android-dialogfragment

我正在尝试将TabHost放在DialogFragment中,但目前它没有功能,并在该行返回null:

tabs.findViewById(R.id.tabHost);

如何在DialogFragment中初始化tabhost以允许显示标签?

选项卡式DialogLog类:

public class InviteFriendTabbedAlertDialog extends DialogFragment {

    TabHost tabs;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //return inflater.inflate(R.layout.invite_friend_tabbed_dialog, null);

        // Add tabs
        tabs.findViewById(R.id.tabHost);

        tabs.setup();

        TabHost.TabSpec tabpage1 = tabs.newTabSpec("one");
        tabpage1.setContent(R.id.shareIndividual);
        tabpage1.setIndicator("Tab 1", getResources().getDrawable(R.drawable.abc_ab_solid_dark_holo));

        TabHost.TabSpec tabpage2 = tabs.newTabSpec("two");
        tabpage2.setContent(R.id.shareGroup);
        tabpage2.setIndicator("Tab 2", getResources().getDrawable(R.drawable.abc_ab_bottom_transparent_light_holo));

        tabs.addTab(tabpage1);
        tabs.addTab(tabpage2);

        return inflater.inflate(R.layout.invite_friend_tabbed_dialog, null);

    }


}

标签式布局文件:

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

    <TabHost
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tabHost"
        android:layout_gravity="center_horizontal">

        <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"></TabWidget>

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

                <LinearLayout
                    android:id="@+id/shareIndividual"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical"></LinearLayout>

                <LinearLayout
                    android:id="@+id/shareGroup"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical"></LinearLayout>

            </FrameLayout>
        </LinearLayout>
    </TabHost>
</RelativeLayout>

1 个答案:

答案 0 :(得分:2)

初始化tabs并使用带有tabHost标签的膨胀视图来初始化tabs变量:

@Override
    public View onCreateView(LayoutInflater inflater, 
                          ViewGroup container, Bundle savedInstanceState) {
      View  view=inflater.inflate(R.layout.invite_friend_tabbed_dialog, null);

        // Add tabs
        tabs=(TabHost)view.findViewById(R.id.tabHost);
        //...your code here
        return view;
    }