2014-02-28 34 views
0

我正在創建一個具有使用片段的導航抽屜的項目。 而我想在第一個片段中出現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; 
    } 
} 

這段代碼的標籤總是出現。

+0

您是否希望在片段打開時才顯示它們? –

+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個選項卡。

+0

不工作,我想只出現在InicioFragment –

+0

我編輯了代碼,現在應該沒有編譯錯誤。如果這仍然不是你想要的,你可以更清楚一點你的意思嗎?我現在不確定我瞭解 – Smittey

+0

這是如何實現每個標籤的代碼? –

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

相關問題