如何使用TabActivity或FragmentActivity在android中创建选项卡

时间:2013-08-14 12:52:18

标签: android tabactivity

我想在android中创建标签,但实际上我不知道如何创建和 刚刚下载了一些视频,但实际上这没有用 我很遗憾地使用TabActivity它已被弃用:( 所以我需要你帮我解决任何建议

1 个答案:

答案 0 :(得分:0)

您必须设置FragmentActivity主类才能运行Fragments。

您需要使用支持库android.support.v4.app中提供的FragmentTabHost类

使用FragmentTabHost的类:

public class SampleTabFragment extends Fragment {

private FragmentTabHost mTabHost;
public static View view;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    // fragment not when container null
    if (container == null) {
        return null;
    }

    // inflate view from layout
    view = (LinearLayout) inflater.inflate(R.layout.offer_tabs, container, false);

    // Setting Up the TabHost
    mTabHost = (FragmentTabHost) view.findViewById(android.R.id.tabhost);
    mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.offer_realtabcontent);

    // For adding a tab divider image
    mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);

    // For Setting up the Tabs with their respective labels
    mTabHost.addTab(mTabHost.newTabSpec("info").setIndicator(createTabView
            (mTabHost.getContext(), "INFO")), DetailedOfferFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("location").setIndicator(createTabView
            (mTabHost.getContext(), "LOCATION")), LocationFragment.class, null);

    setRetainInstance(true);
    return view;
}

/***** Method for Custom TextView for Tabs *****/
private static View createTabView(final Context context, final String text) {
    View view = LayoutInflater.from(context).inflate(R.layout.tabs_text_bg, null);
    TextView tv = (TextView) view.findViewById(R.id.tabsText);
    tv.setText(text);
    return view;
}
}

使用标签的布局offer_tabs.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#6D6C6C"
android:orientation="vertical" >

<android.support.v4.app.FragmentTabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android: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" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:orientation="horizontal" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="2dp"
            android:background="#696969" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />

        <FrameLayout
            android:id="@+id/offer_realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>