How do I define default animations for Navigation Actions?

前端 未结 3 767
情话喂你
情话喂你 2020-12-24 01:32

I\'m using Android Studio 3.2 Canary 14 and The Navigation Architecture Component. With this you can define transition animations pretty much as you would when using Intents

3条回答
  •  醉话见心
    2020-12-24 01:57

    R.anim has the default animations defined (as final):

    • nav_default_enter_anim

    • nav_default_exit_anim

    • nav_default_pop_enter_anim

    • nav_default_pop_exit_anim

    in order to change this behavior, you would have to use custom NavOptions,

    because this is where those animation are being assigned to a NavAction.

    one can assign these with the NavOptions.Builder:

    protected NavOptions getNavOptions() {
    
        NavOptions navOptions = new NavOptions.Builder()
          .setEnterAnim(R.anim.default_enter_anim)
          .setExitAnim(R.anim.default_exit_anim)
          .setPopEnterAnim(R.anim.default_pop_enter_anim)
          .setPopExitAnim(R.anim.default_pop_exit_anim)
          .build();
    
        return navOptions;
    }
    

    most likely one would need to create a DefaultNavFragment, which extends class androidx.navigation.fragment (the documentation there does not seem completed yet).

    So you can pass these NavOptions to the NavHostFragment like this:

    NavHostFragment.findNavController(this).navigate(R.id.your_action_id, null, getNavOptions());
    

    alternatively... when looking at the attrs.xml of that package; these animations are style-able:

    
        
            
            
            
            
            ...
        
    
    

    this means, one can define the according styles - and define these, as part of the theme...

    one can define them in styles.xml:

    
    

提交回复
热议问题