【发布时间】:2012-10-27 16:15:40
【问题描述】:
我使用 Eclipse 项目向导创建了一个带有 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;
}
}
为了我的应用程序的目的,我想在设备处于纵向模式时显示一个片段,而在设备处于横向模式时显示两个片段。我知道莎士比亚样本,但在这个样本中,它是一个包含一个或两个片段的活动。
莎士比亚示例使用两种不同的布局。对于纵向模式(在“layout\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>
对于横向模式(在“layout-land\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 中加载布局,我该如何做同样的事情?
谢谢。
【问题讨论】:
-
我的意思是说赏金中的两个列表视图片段。谢谢你问这个@Ole
标签: android tabs android-actionbar fragment