0

我有一个使用ScrollView垂直滚动的自定义视图,但是当我将它放入android.support.v7.widget.Toolbar时,它将不再滚动。同样的事情发生,如果我也使用NestedScrollView。是否可以在Toolbar内使用ScrollView?如果没有,那么是否有其他方法来实现相同的目标?我可以在Android工具栏中使用ScrollView吗?

下面是我在做什么的一些示例代码:

<?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.me.ExampleActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="@dimen/appbar_padding_top" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:minHeight="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay"> 

      <com.me.NormallyScrollableView 
       android:id="@+id/myView" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" /> 

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

回答

2

总之,不与AppBarLayout。您必须删除它才能在工具栏中获得所需的滚动条。

您的视图层次应该是这样的:

<?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.me.ExampleActivity"> 

    <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/AppTheme.PopupOverlay"> 

     <com.me.NormallyScrollableView 
      android:id="@+id/myView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

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

    <!-- Have to create Margin, since AppBarLayout is not first view in Coordinator layout (which it expects) --> 
    <FrameLayout 
     android:layout_marginTop="?attr/actionBarSize" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent"> 

    </FrameLayout> 

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

但在这一点上,CoordinatorLayout是无用的这个屏幕在你的应用程序...

所以一些了,它不可能您已设置的视图层次结构。

祝你好运,快乐编码!

0

我认为这是不可能做到这一点,但我建议使用此

<?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.me.ExampleActivity"> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingTop="@dimen/appbar_padding_top" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:minHeight="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/AppTheme.PopupOverlay"> 

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

<android.support.v4.widget.NestedScrollView 
android:id="@+id/nestedScrollView" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_marginTop="@dimen/activity_horizontal_margin" 
android:padding="@dimen/activity_horizontal_margin" 
app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

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

相关问题