【问题标题】:Nested fragment improper layout inflation in TabLayoutTabLayout 中的嵌套片段不正确的布局膨胀
【发布时间】:2018-05-15 05:53:07
【问题描述】:

所以我有一个main_activityDrawerLayout。这个活动有FrameLayout,它被fragment_forecast 的布局夸大了。 Fragment_forecast 具有 TabLayout,其中包含其他三个片段。所有这些子片段布局都与TabLayout. 的选项卡重叠

我尝试在 fragment_forecast 中使用 LinearLayout 而不是 FrameLayout,但结果是 child_fragments 的底部被切断了。

处理这种情况的正确方法是什么?

main_activity 的活动布局:

    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.daza.soundmap.Main2Activity">

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="enterAlways"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            />

        <FrameLayout
            android:id="@+id/main_activity_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_view" />

</android.support.v4.widget.DrawerLayout>

fragment_day_forecast(forecast_fragment 的子级)的片段布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.example.daza.soundmap.ForecastFragment">


    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabBackground="@color/primaryLightColor"
        app:tabIndicatorColor="@color/secondaryColor"
        app:tabMode="fixed"
        app:tabSelectedTextColor="@color/primaryTextColor"
        app:tabTextColor="@color/primaryTextColor" />

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
       />

</FrameLayout>

fragment_day_forecast 的片段布局:

<android.support.constraint.ConstraintLayout 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:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.daza.soundmap.DayForecastFragment"
   >

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refresh_layout_hour"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view_hour_forecast"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical" />
    </android.support.v4.widget.SwipeRefreshLayout>

</android.support.constraint.ConstraintLayout>

【问题讨论】:

    标签: android android-fragments android-tablayout


    【解决方案1】:

    试试这组代码。将 FragmentLayout 替换为 RelativeLayout 并在 id tabs 下面设置 viewpager 。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       xmlns:app="http://schemas.android.com/apk/res-auto">
    
    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed" />
    
    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_below="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    
    </RelativeLayout>
    

    【讨论】:

      【解决方案2】:

      TabLayoutViewPager Fragment 重叠,因为您的 fragment_day_forecast 的父级是

      • 框架布局
        • 标签布局
        • ViewPager

      FrameLayout 意味着将一个 View 放在另一个之上。

      您需要使用RelativeLayout 作为父视图。即,设置视图之间的关系。在这种情况下,它将是ViewPager 低于TabLayout。 类似(fragment_day_forecast),

      <RelativeLayout 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"
      tools:context="com.example.daza.soundmap.ForecastFragment">
      
      <android.support.design.widget.TabLayout
          android:id="@+id/tabs"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          app:tabBackground="@color/primaryLightColor"
          app:tabIndicatorColor="@color/secondaryColor"
          app:tabMode="fixed"
          app:tabSelectedTextColor="@color/primaryTextColor"
          app:tabTextColor="@color/primaryTextColor" />
      
      <android.support.v4.view.ViewPager
          android:id="@+id/view_pager"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_below="@id/tabs" />
      </RelativeLayout>
      

      【讨论】:

      • 如果有帮助,请对此答案投票。编码快乐!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-22
      • 1970-01-01
      • 2013-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多