[我使用XML
创建了一个动画,以进行淡入和淡出,并使用进入和退出的导航下拉菜单中的下拉框将其附加到片段上,但是片段没有被动画化。
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/navigation_graph"
app:startDestination="@id/mainFragment">
<fragment
android:id="@+id/mainFragment"
android:name="com.cheapapps.randomquotesmachine.MainFragment"
android:label="fragment_main"
tools:layout="@layout/fragment_main">
<action
android:id="@+id/action_mainFragment_to_quoteDetailFragment"
app:destination="@id/quoteDetailFragment"
app:enterAnim="@anim/fade_in"
app:exitAnim="@anim/fade_out" />
</fragment>
<fragment
android:id="@+id/quoteDetailFragment"
android:name="com.cheapapps.randomquotesmachine.QuoteDetailFragment"
android:label="fragment_quote_detail"
tools:layout="@layout/fragment_quote_detail">
<argument
android:name="position"
android:defaultValue="0"
app:argType="integer" />
</fragment>
</navigation>
我不确定您的fade_in
和fade_out
文件的外观,因此我在下面为您提供示例:
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="500" />
和:
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="1.0" android:toAlpha="0.0"
android:fillAfter="true"
android:duration="500" />
要将这种淡入淡出过渡效果应用于片段,您应该像已经设置的那样设置enterAnim
和exitAnim
,但要像下面的示例一样设置popEnterAnim
和popExitAnim
:
<action android:id="@+id/action_mainFragment_to_quoteDetailFragment"
app:destination="@id/quoteDetailFragment"
app:popEnterAnim="@anim/fade_in"
app:popExitAnim="@anim/fade_out"
app:enterAnim="@anim/fade_in"
app:exitAnim="@anim/fade_out"/>