【问题标题】:Fragment content overlapping toolbar片段内容重叠工具栏
【发布时间】:2017-12-05 12:16:10
【问题描述】:

使用viewpager 和tablayout 实现BottomNavigationView。每个导航视图都是第一个片段(“NewsFeedFragment”)中的一个片段,我想使用带有查看器的tablayout。结果正确,但片段的内容与父 toolbar 重叠。

这是我尝试使用它的方式。

Home.xml

<android.support.design.widget.CoordinatorLayout 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/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.M1Activity">


<android.support.design.widget.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:elevation="6dp">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:elevation="0dp"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

</android.support.design.widget.AppBarLayout>

<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></FrameLayout>


<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="?android:attr/windowBackground"
    app:menu="@menu/navigation" />

NewsFragment.xml

<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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activities.HomeActivity">


<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    app:tabGravity="fill"
    app:tabMode="fixed"
    app:tabSelectedTextColor="@color/colorAccent"
    app:tabTextColor="#FFF" />


<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

这是我得到的结果。

而我基本上想要实现的是。

【问题讨论】:

    标签: java android android-viewpager bottomnavigationview


    【解决方案1】:

    如下图在AppBarLayout里面添加tab布局

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
        tools:context="com.journaldev.tablayoutappbar.MainActivity">
    
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">
    
            <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="scroll|enterAlways"
                app:popupTheme="@style/AppTheme.PopupOverlay" />
    
            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                app:tabGravity="fill"
                app:tabMode="fixed"
                android:layout_height="wrap_content"/>
    
        </android.support.design.widget.AppBarLayout>
    
    
        <android.support.v4.widget.NestedScrollView android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:orientation="vertical">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/sample_text"/>
    
        </LinearLayout>
    
        </android.support.v4.widget.NestedScrollView>
    
    </android.support.design.widget.CoordinatorLayout>
    

    【讨论】:

      【解决方案2】:
      <android.support.design.widget.CoordinatorLayout 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.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:id="@+id/appbar_layout"
          android:layout_width="match_parent"
          android:layout_height="200dp"
          android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
      
          <android.support.design.widget.CollapsingToolbarLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              app:contentScrim="?attr/colorPrimary"
              app:layout_scrollFlags="scroll|exitUntilCollapsed"
              app:titleEnabled="false">
      
      
      Map view is just a view that you want to collapse on scroll replace with your widget
      
      
           <com.google.android.gms.maps.MapView
                  android:id="@+id/mapView"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:scaleType="centerCrop"
                  app:layout_collapseMode="parallax" />
      
      Widget that you want to display after collapse
      
      
          <android.support.design.widget.TabLayout
              android:id="@+id/tab_layout"
           app:layout_collapseMode="pin"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:background="?attr/colorPrimary"
              android:minHeight="?attr/actionBarSize"
              app:tabGravity="fill"
              app:tabMode="fixed"
              app:tabSelectedTextColor="@color/colorAccent"
              app:tabTextColor="#FFF" />
      
      
          <android.support.v4.view.ViewPager
              android:id="@+id/pager"
              app:layout_behavior="@string/appbar_scrolling_view_behavior"
              android:layout_width="match_parent"
              android:layout_height="match_parent" />
          </android.support.design.widget.CollapsingToolbarLayout>
      
      </android.support.design.widget.AppBarLayout>
      
           <android.support.v4.widget.NestedScrollView
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  app:layout_behavior="@string/appbar_scrolling_view_behavior">
      
              All your component to display should be here
      
          </android.support.v4.widget.NestedScrollView>
      
      </android.support.design.widget.CoordinatorLayout >
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-21
        • 1970-01-01
        • 2017-10-01
        • 2015-09-26
        相关资源
        最近更新 更多