【问题标题】:how to put scroll view in collapsing toolbar layout in android?如何将滚动视图放在android中的折叠工具栏布局中?
【发布时间】:2017-03-30 09:46:42
【问题描述】:

我想要类似于下面问的问题,而不是“视图 1”,它将是一个滚动视图: android: Create layout which collapse on scrolling

 <?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.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:fitsSystemWindows="true"
        android:background="#456342"
        android:layout_height="match_parent"
        android:layout_marginBottom="50dp"
        android:clickable="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:theme="@style/AppTheme.AppBarOverlay"
            app:contentScrim="?attr/colorPrimary">



            <android.support.v7.widget.Toolbar
                android:id="@+id/activity_subcard_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                app:layout_collapseMode="pin"/>



            <include layout="@layout/activity_sub_card_content1"
                android:layout_height="200dp"
                android:layout_width="match_parent"/>



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



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

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

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

下面给出的 xml 代码是我的布局“activity_sub_card_content1”,我已包含在我的 CollapsingToolbarLayout 中。我想要类似的东西:

How to place a layout below toolbar in collapsing toolbar layout?

但它的相对布局不太长。我的会很长,所以我想把我的布局放在滚动视图中。

我尝试的代码显示了我的滚动视图,但它不滚动它,而是折叠工具栏。

<ScrollView android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/trial"
    xmlns:android="http://schemas.android.com/apk/res/android">

        <LinearLayout
            android:clickable="true"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v7.widget.CardView
                android:layout_margin="20dp"
                android:layout_width="match_parent"
                android:layout_height="200dp">

            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:layout_margin="20dp"
                android:layout_width="match_parent"
                android:layout_height="200dp">

            </android.support.v7.widget.CardView>
            <android.support.v7.widget.CardView
                android:layout_margin="20dp"
                android:layout_width="match_parent"
                android:layout_height="200dp">

            </android.support.v7.widget.CardView>
            <android.support.v7.widget.CardView
                android:layout_margin="20dp"
                android:layout_width="match_parent"
                android:layout_height="200dp">

            </android.support.v7.widget.CardView>




        </LinearLayout>

    </ScrollView>

【问题讨论】:

    标签: android android-layout scrollview android-coordinatorlayout android-collapsingtoolbarlayout


    【解决方案1】:

    您可以尝试在代码中添加这两行:

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        >
    <Your code here with 
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    </android.support.v4.widget.NestedScrollView>
    

    【讨论】:

      【解决方案2】:

      你可以尝试添加这个标签吗

          app:layout_behavior="@string/appbar_scrolling_view_behavior"
      
          <ScrollView android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:id="@+id/trial"
          app:layout_behavior="@string/appbar_scrolling_view_behavior"
              xmlns:android="http://schemas.android.com/apk/res/android">
      
      ----- your code
      
      </ScrollView>
      

      AppBarLayout 还需要一个单独的滚动兄弟,以便知道何时滚动。绑定是通过 AppBarLayout.ScrollingViewBehavior 类完成的,这意味着您应该将滚动视图的行为设置为 AppBarLayout.ScrollingViewBehavior 的实例。包含完整类名的字符串资源可用。

      查看此链接: https://developer.android.com/reference/android/support/design/widget/AppBarLayout.html

      【讨论】:

        猜你喜欢
        • 2016-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-03
        • 2015-08-14
        相关资源
        最近更新 更多