【问题标题】:How to make toolbar fixed while scrolling in android?如何在android中滚动时固定工具栏?
【发布时间】:2017-06-25 15:47:10
【问题描述】:

在发布这个问题之前,我已经研究了所有以前的问题,以前的解决方案都没有对我有用。我需要在滚动时将我的Toolbar 设为静态,在滚动时它会上升,但我需要将其设为静态我该怎么做。现在让我解释一下我到目前为止尝试过的所有方法,

  1. 我已经输入windowSoftInputMode调整resize

  2. fitwindows 真正的布局

这是我写的代码:

这是主CoordinatorLayout

<?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"
    android:id="@+id/main_content"
    android:fitsSystemWindows="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

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

    <include layout="@layout/activity_incident"
       />



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

这是布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_incident"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
    <android.support.v7.widget.RecyclerView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/incident_recyclerview"
        android:divider="#fff"
         android:keepScreenOn="true"
        android:dividerHeight="0.5dp"
        />
</RelativeLayout>

我只是想让我的Toolbar 静态我怎样才能实现提前谢谢!!

【问题讨论】:

  • @M.Yogeshwaran 您想将工具栏保持在同一位置,或者这又意味着什么使其成为静态
  • 与布局无关,我不能与协调器布局一起使用
  • @Charuක 你好,我不希望工具栏移动它需要是静态的
  • 在 AppBarLayout 之后添加 Nested ScrollView 就可以了。

标签: android material-design toolbar android-scroll


【解决方案1】:

Toolbar 中删除app:layout_scrollFlags="scroll|enterAlways" 并检查滚动。

【讨论】:

    【解决方案2】:

    好吧,有很多答案,但是,

    这是你要找的吗?

    如果是,您可以使用 CollapsingToolbarLayout

    对于第二个视图,这里是我的 xml,您只需删除 ImageView 即可获得第一个视图。

    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        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:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">
    
                <ImageView
                    android:id="@+id/profile_id"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/monkey"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerCrop"
                    app:layout_collapseMode="parallax" />
    
                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin" />
    
            </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"
            android:background="#ffe5e5e5"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
    
                <include layout="@layout/activity_second" />
    
            </LinearLayout>
    
    
        </android.support.v4.widget.NestedScrollView>
    
    </android.support.design.widget.CoordinatorLayout>
    

    请注意,我将app:layout_collapseMode="pin" 用于Toolbar

    玩得开心^_^

    【讨论】:

    • @M.Yogeshwaran 你检查过这个答案吗
    【解决方案3】:

    让你的布局像这样。当您滚动布局时,您的工具栏将固定在顶部。

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:fitsSystemWindows="true"
        >
    
        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|enterAlwaysCollapsed"
            app:statusBarScrim="@color/primary"
            app:contentScrim="@color/primary"
            app:titleEnabled="false"
            app:layout_collapseMode="none"
            android:fitsSystemWindows="true">
    
    
            <!--
            we must create the value entry for the toolbar height here as different version would have different heights
            -->
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="top"
                android:gravity="top"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:contentInsetLeft="0dp"
                app:contentInsetStart="0dp">
    
    
            </android.support.v7.widget.Toolbar>
    
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>
    

    【讨论】:

      【解决方案4】:
          <?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"
          android:id="@+id/main_content"
          android:fitsSystemWindows="true"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
      
          <android.support.design.widget.AppBarLayout
              android:id="@+id/appbar"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              app:layout_behavior="@string/appbar_scrolling_view_behavior">
      
              <android.support.v7.widget.Toolbar
                  android:id="@+id/toolbar"
                  android:layout_width="match_parent"
                  android:layout_height="?attr/actionBarSize"
                  android:background="?attr/colorPrimary"
                  app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                  app:layout_scrollFlags="scroll|enterAlways" />
          </android.support.design.widget.AppBarLayout>
      
          <include layout="@layout/activity_incident"
             />
      
      
      
      </android.support.design.widget.CoordinatorLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-19
        • 2022-11-16
        • 1970-01-01
        • 2016-01-26
        相关资源
        最近更新 更多