【发布时间】:2016-01-30 14:59:38
【问题描述】:
所以我在我的应用程序中使用 ViewPager 实现了选项卡布局。每个选项卡显示一个片段。现在我想从选项卡布局中的这些片段之一打开一个新片段(我们称之为 B)(我们称之为 A)。所以 A 在选项卡布局中,但我希望 B 占据整个屏幕。我什至能够做到,但我面临的问题很小。新片段不会占据整个屏幕。相反,它只是替换了前一个片段,看起来它是选项卡布局的一部分。
这是片段 A 的布局(我从中启动新片段)。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/parent">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="0dp" />
</FrameLayout>
</LinearLayout>
在片段 A 的类中,我使用这些行启动新片段,
Bundle bundle = new Bundle();
FragmentTransaction fragmentTransaction = getActivity()
.getSupportFragmentManager()
.beginTransaction();
Fragment fragmentB = new FragmentB();
fragmentB.setArguments(bundle);
fragmentTransaction.replace(R.id.parent, fragmentB);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
也是我的主要活动,这些片段附加到使用 viewPager 实现 tavLayout。
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@id/tab_layout"/>
我最终看到了一个新片段(片段 B),但作为选项卡布局的一部分,我想隐藏选项卡布局。请帮助我,我经历了许多 SO 问题,但我觉得我做错了一些事情,因为它们对我不起作用。谢谢!!
【问题讨论】:
-
子片段如何比父片段占用更多空间??
标签: android android-fragments android-viewpager android-tablayout