嵌套片段在TabLayout中不正确的布局膨胀

时间:2018-05-15 05:53:08

标签: android android-fragments android-tablayout

所以我有main_activity DrawerLayout。此活动的FrameLayoutfragment_forecast的布局。 Fragment_forecastTabLayout,其中包含其他三个片段。所有这些子片段布局都与TabLayoutenter image description here

的标签重叠

我尝试在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>

2 个答案:

答案 0 :(得分:1)

尝试这一堆代码。将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>

答案 1 :(得分:0)

TabLayoutViewPager Fragment重叠,因为fragment_day_forecast的父母是

  • 的FrameLayout
    • TabLayout
    • ViewPager

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

您需要使用ViewPager作为父视图。即,设置视图之间的关系。在这种情况下,它会TabLayout低于<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> 。 类似于(fragment_day_forecast),

Asp.Net Core 2.x