首页 文章

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

提问于
浏览
0

所以我有 main_activityDrawerLayout . 此活动具有 FrameLayout ,其使用 fragment_forecast 的布局进行了充气 . Fragment_forecastTabLayout ,其中包含其他三个片段 . 所有这些子片段布局都与 TabLayout 的选项卡重叠 .
enter 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 回答

  • 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>
    
  • 0

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

    • FrameLayout

    • TabLayout

    • ViewPager

    FrameLayout 的意思是将一个 View 放在另一个 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>
    

相关问题