开发技巧Android开发中遇到的问题Android常见问题

解决RecyclerView把Toolber遮住的问题

2018-02-25  本文已影响57人  亚欧沙龙

RecyclerView把Toolber遮住了

惊恐

抓狂

问题就像这张图片一样,来的太快,就像龙卷风....... 抓狂

我们来分析一下为什么RecyclerView会把Toolber遮住吧

1,首先看一下布局文件的代码

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_height="match_parent"
android:layout_width="match_parent" >

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="16dp"
        android:src="@drawable/ic_done"
        android:elevation="8dp"/>
    <com.example.lab.android.nuc.materialtest.DragFloatActionButton
        android:id="@+id/fab_move"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|start"
        android:layout_margin="16sp"
        android:src="@drawable/ic_add"/>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_gravity="start"
    app:menu="@menu/nav_menu"
    app:headerLayout="@layout/nav_header" />

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

想必大家都很清楚

CoordinatorLayout 就是加强版的FrameLayout</pre>

抓狂 而FrameLayout中的控件在不进行明确定位的情况下,默认都会摆放到左上角** 抓狂

****那么该如何解决呢?****

1. 传统情况下,使用偏移是唯一的解决办法 惊恐

2. 真叫人脑阔疼 惊恐

3. 但是:

我们使用的可不是普通的FrameLayout,而是CoordinatorLayout,因此自然会有一些更加巧妙地方法了 微笑
微笑 微笑

即:Design support库中提供的另外的一个工具——AppBarLayout

那么我们怎样用AppBarLayout才能解决前面的覆盖问题呢?

其实只需要两步就可以了:

1.将ToolBar嵌套到AppBarLayout中

2.给RecyclerView指定一个布局行为

布局代码更改为:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_height="match_parent"
android:layout_width="match_parent" >

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="16dp"
        android:src="@drawable/ic_done"
        android:elevation="8dp"/>
    <com.example.lab.android.nuc.materialtest.DragFloatActionButton
        android:id="@+id/fab_move"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|start"
        android:layout_margin="16sp"
        android:src="@drawable/ic_add"/>
</android.support.design.widget.CoordinatorLayout>

<!--<TextView-->
    <!--android:layout_width="match_parent"-->
    <!--android:layout_height="match_parent"-->
    <!--android:layout_gravity="start"-->
    <!--android:text="This is menu"-->
    <!--android:textSize="30sp"-->
    <!--android:background="#FFF"/>-->

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_gravity="start"
    app:menu="@menu/nav_menu"
    app:headerLayout="@layout/nav_header" /></android.support.v4.widget.DrawerLayout>

简化一下就是:

<android.support.design.widget.CoordinatorLayout
       
       <android.support.design.widget.AppBarLayout
             <android.support.v7.widget.Toolbar
       </android.support.design.widget.AppBarLayout>

       <android.support.v7.widget.RecyclerView
                                   <app:layout_behavior="@string/appbar_scrolling_view_behavior"/>    
  
</android.support.design.widget.CoordinatorLayout>

效果如图:

上一篇 下一篇

猜你喜欢

热点阅读