Android - 将ActionBar / Toolbar保留在Fragment动画前面

时间:2016-03-24 15:38:51

标签: android animation android-fragments

我使用translate动画从底部向下滑动片段。但是,在此动画期间,ToolbarActionBar被发送到后面,使其看起来像动画中的某种故障(动画Fragment涵盖了Toolbar)。

有没有办法确保Toolbar始终显示在动画前面?我确实尝试将:zAdjustment设置为bottom,但这并没有改变任何内容。

这是动画中的一个。其他只是:fromYDelta:toYDelta的变体。

<set>
    <translate
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromYDelta="-100%"
        android:toYDelta="0"
        android:interpolator="@android:anim/decelerate_interpolator"
        android:duration="1000"/>
</set>

FragmentTransaction

getSupportFragmentManager().beginTransaction()
        .setCustomAnimations(R.anim.slide_in_bottom, R.anim.slide_out_top, R.anim.slide_in_top, R.anim.slide_out_bottom)
        .replace(R.id.master_container, getManageFragment())
        .addToBackStack(null)
        .commit();

这是我的activity_main.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <!-- The main content view -->
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/master_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

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

    </LinearLayout>

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

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

然后将此布局添加到master_container

<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:background="@color/global_background"
    app:layout_behavior="com.cohenadair.anglerslog.views.FloatingButtonBehavior">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/main_recycler_view"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:paddingTop="@dimen/margin_half"
        android:paddingBottom="@dimen/margin_half"
        android:clipToPadding="false"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/new_button"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_margin="@dimen/margin_default"
        android:src="@drawable/ic_add"
        app:elevation="0dp"
        app:layout_anchor="@id/main_recycler_view"
        app:layout_anchorGravity="bottom|right|end"
        app:layout_behavior="com.cohenadair.anglerslog.utilities.ScrollingFabBehavior"/>

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

谢谢。

编辑:This似乎是一个类似的问题。

1 个答案:

答案 0 :(得分:0)

如果您在Toolbar中设置Activity,则FrameLayout中的转换不会影响工具栏。我试过这样的&amp;它的工作

<android.support.v4.widget.DrawerLayout
    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/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:openDrawer="start">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:contentInsetStartWithNavigation="0dp"
            app:layout_collapseMode="pin"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            android:title="@string/app_name"
            android:background="@color/colorPrimaryDark"
            app:contentInsetStartWithNavigation="0dp"/>

        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/toolbar"/>
    </RelativeLayout>
    <include layout="@layout/navigation_drawer_layout"/>
</android.support.v4.widget.DrawerLayout>

动画:

getSupportFragmentManager().beginTransaction()
                    .setCustomAnimations(R.anim.enter_from_top, R.anim.exit_to_bottom, R.anim.enter_from_bottom, R.anim.exit_to_top)
                    .replace(R.id.fragment_container, fragment, fragment.getClass().getSimpleName())
                    .commitAllowingStateLoss();

enter_from_bottom.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false">
    <translate
        android:fromXDelta="0%" android:toXDelta="0%"
        android:fromYDelta="100%" android:toYDelta="0%"
        android:duration="700" />
</set>

enter_from_top.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false">
    <translate
        android:fromXDelta="0%" android:toXDelta="0%"
        android:fromYDelta="-100%" android:toYDelta="0%"
        android:duration="700"/>
</set>

exit_to_bottom.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false">
    <translate
        android:fromXDelta="0%" android:toXDelta="0%"
        android:fromYDelta="0%" android:toYDelta="100%"
        android:duration="700" />
</set>

exit_to_top.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false">
    <translate
        android:fromXDelta="0%" android:toXDelta="0%"
        android:fromYDelta="0%" android:toYDelta="-100%"
        android:duration="700"/>
</set>