简体   繁体   English

滚动时,CollapsingToolbarLayout未折叠

[英]CollapsingToolbarLayout not collapsed when scrolled

1 I´m trying to use CollapsingToolbarLayout with a ScrollView but I do not why it doesn't work. 1我试图使用ScrollView使用CollapsingToolbarLayout,但我不知道为什么它不起作用。 I try this: 我试试这个:

<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:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="256dp">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsingToolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar"
            app:layout_scrollFlags="scroll|enterAlways"></include>

        <com.github.mikephil.charting.charts.LineChart
            android:id="@+id/graph"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/toolbar"
            android:background="@color/white"></com.github.mikephil.charting.charts.LineChart>
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

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

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

my nested scrollview that its bhavior set to appbar_scrolling_view_behavior 我的嵌套scrollview,它的bhavior设置为appbar_scrolling_view_behavior

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="10dp">
<include layout="@layout/scroll_view_aba"/>
<include layout="@layout/scroll_view_aci"/>
<include layout="@layout/scroll_view_aci_reduced"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>

my nestedScrollView is scrolled but the collapsing toolbar is pinned! 我的nestedScrollView已滚动但折叠工具栏已固定!

  • make sure you are using toolbar as actionbar To use Toolbar as an ActionBar, first ensure the AppCompat-v7 support library is added to your application build.gradle (Module:app) file: 确保使用工具栏作为操作栏要将工具栏用作ActionBar,请首先确保将AppCompat-v7支持库添加到应用程序build.gradle (Module:app)文件中:

      dependencies { ... compile 'com.android.support:appcompat-v7:23.1.0' } 

    Second, let's disable the theme-provided ActionBar. 其次,让我们禁用主题提供的ActionBar。 The easiest way is to have your theme extend from Theme.AppCompat.NoActionBar (or the light variant) within the res/styles.xml file: 最简单的方法是让您的主题从res/styles.xml文件中的Theme.AppCompat.NoActionBar (或light变量)扩展:

     <resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> </style> </resources> 

    Now you need to add a Toolbar to your Activity layout file. 现在,您需要将Toolbar添加到Activity布局文件中。 One of the biggest advantages of using the Toolbar widget is that you can place the view anywhere within your layout. 使用工具栏小部件的最大优势之一是您可以将视图放置在布局中的任何位置。 Below we place the toolbar at the top of a LinearLayout like the standard ActionBar: 下面我们将工具栏放在LinearLayout的顶部,就像标准的ActionBar一样:

     <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:minHeight="?attr/actionBarSize" android:layout_width="match_parent" android:layout_height="wrap_content" app:titleTextColor="@android:color/white" android:background="?attr/colorPrimary"> </android.support.v7.widget.Toolbar> <!-- Layout for content is here. This can be a RelativeLayout --> </LinearLayout> 

    Note: You'll want to add android:fitsSystemWindows="true" to the parent layout of the Toolbar to ensure that the height of the activity is calculated correctly. 注意:您需要将android:fitsSystemWindows="true"到工具栏的父布局,以确保正确计算活动的高度。

As Toolbar is just a ViewGroup and can be styled and positioned like any other view . 由于工具栏只是一个ViewGroup ,可以像任何其他视图一样进行样式设置和定位 Note that this means if you are in a RelativeLayout, you need to ensure that all other views are positioned below the toolbar explicitly. 请注意,这意味着如果您在RelativeLayout中,则需要确保所有其他视图明确位于工具栏下方。 The toolbar is not given any special treatment as a view. 工具栏未作为视图进行任何特殊处理。

Next, in your Activity or Fragment, set the Toolbar to act as the ActionBar by calling the setSupportActionBar(Toolbar) method: 接下来,在Activity或Fragment中,通过调用setSupportActionBar(Toolbar)方法将工具栏设置为ActionBar:

Note: When using the support library, make sure that you are importing android.support.v7.widget.Toolbar and not android.widget.Toolbar. 注意:使用支持库时,请确保您要导入android.support.v7.widget.Toolbar而不是android.widget.Toolbar.

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

public class MyActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        // Find the toolbar view inside the activity layout
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        // Sets the Toolbar to act as the ActionBar for this Activity window.
        // Make sure the toolbar exists in the activity and is not null
        setSupportActionBar(toolbar);
    }

    // Menu icons are inflated just as they were with actionbar
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
}

Next, we need to make sure we have the action items listed within a menu resource file such as res/menu/menu_main.xml which is inflated above in onCreateOptionsMenu: 接下来,我们需要确保我们在菜单资源文件中列出了操作项,例如res/menu/menu_main.xml ,它在onCreateOptionsMenu上面膨胀:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/miCompose"
        android:icon="@drawable/ic_compose"
        app:showAsAction="ifRoom"
        android:title="Compose">
    </item>
    <item
        android:id="@+id/miProfile"
        android:icon="@drawable/ic_profile"
        app:showAsAction="ifRoom|withText"
        android:title="Profile">
    </item>
</menu>

From this point on, all menu items are displayed in your Toolbar, populated via the standard options menu callbacks. 从此时起,所有菜单项都显示在工具栏中,通过标准选项菜单回调填充。

  • Also make sure that the CoordinatorLayout is the main layout container. 还要确保CoordinatorLayout是主布局容器。

      <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:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> </android.support.design.widget.CoordinatorLayout> 

I found my answer in my toolbar XML file ,layout_height is set to "wrap_content". 我在工具栏XML文件中找到了答案,layout_height设置为“wrap_content”。 I set it to "?attr/actionBarSize" and CollapsingToolbarLayout scrolled charmly 我把它设置为“?attr / actionBarSize”,并且CollapsingToolbarLayout很好地滚动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM