简体   繁体   中英

How to disable Toolbar fade-in animation?

Is there a way to disable the fade-in / fade-out animation for ToolBar elements? I found a solution for the status bar:

getWindow().getEnterTransition().excludeTarget(android.R.id.statusBarBackground, true);

But I can not find a similar fix for the ToolBar . Everytime I load a new Activity, it causes an undesired flash.

Edit:

styles.xml

<resources xmlns:tools="http://schemas.android.com/tools">

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Status Bar Color -->
        <item name="android:colorPrimaryDark" tools:targetApi="21">@color/primary_4</item>
    </style>

    <!-- Toolbar Style -->
    <style name="toolbar">
        <item name="android:textColorPrimary">@color/primary_0</item>
        <item name="colorControlNormal">@color/primary_0</item>
        <item name="colorControlHighlight">@color/primary_3</item>
    </style>

</resources>

In activity_main.xml & activity_details.xml

 <android.support.v7.widget.Toolbar
        xmlns:app="http://schemas.android.com/apk/res-auto"
        app:theme="@style/toolbar"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?android:attr/actionBarSize"
        android:background="@color/primary_4" />

The Elevation animation duration value is R.integer.app_bar_elevation_anim_duration which by default is 150 .

To avoid the animation, just put following into any xml under res/values and that's it:

<integer name="app_bar_elevation_anim_duration" tools:override="true">0</integer>

The documentation for excludeTarget() says:

public Transition excludeTarget (View target, boolean exclude) :

Whether to add the given target to the list of targets to exclude from this transition. The exclude parameter specifies whether the target should be added to or removed from the excluded list.

Excluding targets is a general mechanism for allowing transitions to run on a view hierarchy while skipping target views that should not be part of the transition. For example, you may want to avoid animating children of a specific ListView or Spinner. Views can be excluded either by their id, or by their instance reference, or by the Class of that view

This means that the way you excluded the status bar from the enter transition would also work for the toolbar. I tried it and it's working.

secondActivity.xml:

...
<include
            android:id="@+id/customToolbar"
            layout="@layout/toolbar" />
...

SecondActivity.java (Inside onCreate()):

...
getWindow().getEnterTransition().excludeTarget(R.id.customToolbar, true);
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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