我使用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中加載佈局?
謝謝。
我打算在賞金中說兩個listview片段。感謝您提問@Ole – danny117