【问题标题】:TabLayout and ViewPager disappearing after Orientation Change方向更改后 TabLayout 和 ViewPager 消失
【发布时间】:2020-05-15 16:20:56
【问题描述】:

目前,我正在开发对不同方向的支持。我的横向布局有一个 viewpager 和 tablayout,纵向布局只有一个片段。当我以任何方向启动应用程序时,布局都可以正常工作。但是,当在运行时改变方向时,在这种情况下从纵向到横向,viewpager 和 tablayout 完全从屏幕上消失,即使分配了权重,也只留下父级高度和宽度的片段。

这让我相信布局没有正确更改。我也没有将 android:configurations 应用于我的清单。否则,当我改变它们的方向时,其他活动和片段可以正常工作。出于某种原因,只有这不起作用。我将在下面附上 XML 代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer"
tools:context=".HomeActivity">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    tools:ignore="UselessParent">
    <fragment
        android:id="@+id/none"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:tag="fragment_tag"
        android:name="com.example.trackeths.HomeFragment" />
</FrameLayout>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_weight="1">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="fill"
        app:tabMode="fixed"
        app:tabTextColor="#fff"
        app:tabIndicatorColor="#fff"
        android:background="@color/colorPrimaryDark"
        />

    <androidx.viewpager.widget.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewPager"/>

</LinearLayout>

【问题讨论】:

    标签: android android-fragments android-viewpager


    【解决方案1】:

    发布此问题后,突然顿悟,它可能是覆盖我的布局的持久片段(即,由于已保存实例状态,横向模式下的纵向模式堆栈中的片段)。

    因此,将 super 调用更改为 null。

    super.onCreate(null);
    

    如果有人感兴趣或者像我一样对 Fragment 不熟悉,可以从 Fool-proof way to handle Fragment on orientation change 那里得到这个想法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多