2012-11-07 48 views
3

我使用Eclipse項目嚮導來創建帶有ActionBar和選項卡的項目。該向導會爲每個選項卡創建一個虛擬片段,其中僅顯示選項卡編號的虛擬文本。該應用程序工作沒有問題。顯示ActionBar選項卡的兩個片段

的代碼看起來是這樣的:

@Override 
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
     // When the given tab is selected, show the tab contents in the container 

     Fragment fragment = new DummySectionFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, tab.getPosition() + 1); 
     fragment.setArguments(args); 
     getFragmentManager().beginTransaction() 
       .replace(R.id.container, fragment) 
       .commit(); 
    } 

    /** 
    * A dummy fragment representing a section of the app, but that simply displays dummy text. 
    */ 
    public static class DummySectionFragment extends Fragment { 
     public DummySectionFragment() { 
     } 

     public static final String ARG_SECTION_NUMBER = "section_number"; 



     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      TextView textView = new TextView(getActivity()); 
      textView.setGravity(Gravity.CENTER); 
      Bundle args = getArguments(); 
      textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER))); 
      return textView; 
     } 
    } 

我的應用程序的目的,我想展示一個片段,當設備處於縱向模式和兩個片段,當設備處於橫向模式。我知道莎士比亞的樣本,但在這個樣本中,它是一個持有一個或兩個碎片的活動。

莎士比亞樣本使用兩種不同的佈局。對於肖像模式(在 「佈局\ fragment_layout_support.xml」):

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent"> 
    <fragment class=".FragmentLayoutSupport$TitlesFragment" 
      android:id="@+id/titles" 
      android:layout_width="match_parent" android:layout_height="match_parent" /> 
</FrameLayout> 

和風景模式(在 「佈局土地\ fragment_layout_support.xml」):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" android:layout_height="match_parent"> 

    <fragment class=".FragmentLayoutSupport$TitlesFragment" 
      android:id="@+id/titles" android:layout_weight="1" 
      android:layout_width="0px" android:layout_height="match_parent" /> 

    <FrameLayout android:id="@+id/details" android:layout_weight="1" 
      android:layout_width="0px" android:layout_height="match_parent" /> 

</LinearLayout> 

莎士比亞樣品加載這樣的佈局:

public class FragmentLayoutSupport extends FragmentActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     //setTheme(SampleList.THEME); //Used for theme switching in samples 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.fragment_layout_support); 
    } 
... 

如何對我的應用程序執行相同的操作,但無法在FragmentActivity中加載佈局?

謝謝。

+0

我打算在賞金中說兩個listview片段。感謝您提問@Ole – danny117

回答

1

也許你被卡住了,因爲你不知道ActionBarActivity繼承FragmentActivity

所以,簡單地用

extends ActionBarActivity 

更換

extends FragmentActivity 

和正確的進口(見ActionBar API Guide)。然後,您將能夠實現與莎士比亞樣本中相同的行爲,同時保持您的ActionBar/Tabs。只需在之前的工作活動中擴展FragmentLayoutSupport而不是ActionBarActivity。

+0

感謝您的回覆。我們團隊的另一位開發人員去年解決了這個問題 - 自那時以來,代碼中發生了很多變化,所以我很遺憾無法說出它是如何解決的。建議的答案似乎是一個非常好的建議,可能是錯誤的。所以我會給大家一個答案。 –

+0

甚至沒有看日期!我只是堅持了一個小時,不知道那時我回答了我在解決過程中發現的這個問題。 – florentbuisson

相關問題