TabLayout 中的嵌套片段布局不当 inflation
Nested fragment improper layout inflation in TabLayout
所以我有一个 main_activity
和 DrawerLayout
。这个 activity 有 FrameLayout
,用 fragment_forecast
的布局膨胀。 Fragment_forecast
有 TabLayout
其中包含另外三个片段。所有这些 child 片段布局都与 TabLayout
.
的选项卡重叠
我尝试在 fragment_forecast 中使用 LinearLayout 而不是 FrameLayout,但结果是 child_fragments 的底部被截断了。
处理这种情况的正确方法是什么?
Activity 布局 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
(child/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>
试试这组代码。将 FragmentLayout 替换为 RelativeLayout 并将 viewpager 设置在 id tabs 下方。
<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>
TabLayout
与 ViewPager
Fragment
重叠,因为 fragment_day_forecast
的 parent 是
- 框架布局
- TabLayout
- ViewPager
其中 FrameLayout
是指将一个 View
放在另一个之上。
您需要使用 RelativeLayout
作为 parent 视图。即,设置视图之间的关系。在这种情况下,它将是 TabLayout
下面的 ViewPager
。
像(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>
所以我有一个 main_activity
和 DrawerLayout
。这个 activity 有 FrameLayout
,用 fragment_forecast
的布局膨胀。 Fragment_forecast
有 TabLayout
其中包含另外三个片段。所有这些 child 片段布局都与 TabLayout
.
我尝试在 fragment_forecast 中使用 LinearLayout 而不是 FrameLayout,但结果是 child_fragments 的底部被截断了。
处理这种情况的正确方法是什么?
Activity 布局 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
(child/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>
试试这组代码。将 FragmentLayout 替换为 RelativeLayout 并将 viewpager 设置在 id tabs 下方。
<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>
TabLayout
与 ViewPager
Fragment
重叠,因为 fragment_day_forecast
的 parent 是
- 框架布局
- TabLayout
- ViewPager
其中 FrameLayout
是指将一个 View
放在另一个之上。
您需要使用 RelativeLayout
作为 parent 视图。即,设置视图之间的关系。在这种情况下,它将是 TabLayout
下面的 ViewPager
。
像(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>