Android:对于不同的设备方向,如何混合ViewPager布局和非ViewPager布局?

Android:对于不同的设备方向,如何混合ViewPager布局和非ViewPager布局?,android,android-layout,Android,Android Layout,目标是根据屏幕方向(纵向或横向),在屏幕上以不同的布局显示两个片段(碎片A、碎片B) 当设备处于纵向模式时,使用滑动在UI之间切换,一次仅显示Frag A和Frag B中的一个。ViewPager非常适合这种情况 当设备处于横向模式时,屏幕左侧显示Frag A,右侧显示Frag B。简单的线性布局非常适合于此 当试图使两者在同一个应用程序中工作时,会出现问题。看起来必须在Java源代码中引用ViewPager类(在onCreate方法中)。这意味着在XML和源代码中都定义了UI元素。。。当设备处

目标是根据屏幕方向(纵向或横向),在屏幕上以不同的布局显示两个片段(碎片A、碎片B)

当设备处于纵向模式时,使用滑动在UI之间切换,一次仅显示Frag A和Frag B中的一个。ViewPager非常适合这种情况

当设备处于横向模式时,屏幕左侧显示Frag A,右侧显示Frag B。简单的线性布局非常适合于此

当试图使两者在同一个应用程序中工作时,会出现问题。看起来必须在Java源代码中引用ViewPager类(在onCreate方法中)。这意味着在XML和源代码中都定义了UI元素。。。当设备处于横向模式时,这似乎是一个问题,不应该有ViewPager对象,但源代码中提到了一个。我的做法正确吗?如果一个版面正在使用ViewPager,是否其他版面都需要ViewPager

布局/活动\u mymain.xml

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MyMainActivity" >

    <!--
    This title strip will display the currently visible page title, as well as the page
    titles for adjacent pages.
    -->

    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#33b5e5"
        android:paddingBottom="4dp"
        android:paddingTop="4dp"
        android:textColor="#fff" />

</android.support.v4.view.ViewPager>

ViewPager用于在运行时实例化视图对象,因此需要在代码中定义它

我尝试做的与中描述的不同平板电脑/手机布局非常相似,他们建议使用单独的活动

(如果有人知道完全用XML处理刷卡的方法,那将是一个更好的解决方案)

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

    <fragment android:name="com.example.ActionFragment"
              android:id="@+id/action_fragment"
              android:layout_weight="1"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

    <fragment android:name="com.example.SummaryFragment"
              android:id="@+id/summary_fragment"
              android:layout_weight="2"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

</LinearLayout>
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_topmain);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

}