如何在片段中创建选项卡

时间:2014-02-28 15:32:35

标签: java android android-fragments tabs fragment

我正在创建一个使用片段的导航抽屉的项目。 我想在第一个片段中出现3个标签。 但我无法创建显示在InicioFragment.class中的选项卡。我该怎么办?

InicioFragment.class

package com.menuguru;

public class InicioFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {   
        View rootView = inflater.inflate(R.layout.fragment_inicio, container, false);
        // getActivity().getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);


        return rootView;
    }   
}

使用此代码,标签始终显示。

2 个答案:

答案 0 :(得分:0)

在OnCreateView方法中,您需要以编程方式设置选项卡。像这样:

    TabHost host = (TabHost) root.findViewById(R.id.tab_host);
    host.setup();

    TabSpec spec = host.newTabSpec("Tab One");
    spec.setContent(R.id.tab_one_container);
    spec.setIndicator("One");
    host.addTab(spec);

    spec = host.newTabSpec("Tab Two");
    spec.setContent(R.id.tab_two_container);
    spec.setIndicator("Two");
    host.addTab(spec);

    spec = host.newTabSpec("Tab Three");
    spec.setContent(R.id.tab_three_container);
    spec.setIndicator("Three");
    host.addTab(spec);


    host.setCurrentTab(0);

在你的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:orientation="vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp" >

<TabHost
    android:id="@+id/tab_host"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="25dp" >

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

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

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

            <LinearLayout
                android:id="@+id/tab_one_container"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:text="One" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:layout_marginLeft="20dp"
                    android:text="Test1" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab_two_container"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:text="Two" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:layout_marginLeft="20dp"
                    android:text="Test2" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab_three_container"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:text="Three" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:layout_marginLeft="20dp"
                    android:text="Test3" />
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>

</LinearLayout>

这应该设置3个标签。

答案 1 :(得分:0)

首先,使用以下模式创建片段:

public class TestFragment extends SherlockFragment {
    private String mContent = "???";

 public static TestFragment newInstance(String text) {
    TestFragment fragment = new TestFragment();

    // Supply num input as an argument.
    Bundle args = new Bundle();
    args.putString(KEY_TAB_NUM, text);
    fragment.setArguments(args);

    return fragment;
 }

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_main, null);
    String text = getString(R.string.tab_page_num) + mContent;
    ((TextView)view.findViewById(R.id.text)).setText(text);

    return view;
 }

 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContent =  getArguments() != null ? getArguments().getString(KEY_TAB_NUM) : "???";
 }
}     

然后在您的主要活动中声明片段如下:

    public class VpiAbsTestActivity extends SherlockFragmentActivity {

    private static final String[] TAB_TITLES = new String[] { "This", "Is", "A", "ViewPager" };

    TestFragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.simple_tabs);

    mAdapter = new TestFragmentAdapter(getSupportFragmentManager());

    mPager = (ViewPager)findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);

    mIndicator = (TabPageIndicator)findViewById(R.id.indicator);
    mIndicator.setViewPager(mPager);
}

 class TestFragmentAdapter extends FragmentPagerAdapter {     
 private int mCount = TAB_TITLES.length;

 public TestFragmentAdapter(FragmentManager fm) {
     super(fm);
 }

 @Override
 public Fragment getItem(int position) {
     return TestFragment.newInstance(String.valueOf(position));
 }

 @Override
 public int getCount() {
     return mCount;
 }

 @Override
 public CharSequence getPageTitle(int position) {
  return TAB_TITLES[position];
 }
      }
   }

最后在AndroidManifest中声明:

    <activity android:name="VpiAbsTestActivity" android:theme="@style/Theme.VPI">

我已成功实施此示例,例如here

相关问题