【问题标题】:Fragment transitions defined in xml do not workxml 中定义的片段转换不起作用
【发布时间】:2019-01-15 07:39:06
【问题描述】:

我在 values-21/styles.xml 中添加了片段转换,如下所示:

    <item name="android:fragmentEnterTransition">@transition/window_enter</item>
    <item name="android:fragmentExitTransition">@transition/window_exit</item>
    <item name="android:fragmentReturnTransition">@transition/window_return</item>
    <item name="android:fragmentReenterTransition">@transition/window_reenter</item>

transition/window_enter.xml

<?xml version="1.0" encoding="utf-8"?>
<slide />

transition/window_exit

<?xml version="1.0" encoding="utf-8"?>
<slide/>

transition/window_return

<?xml version="1.0" encoding="utf-8"?>
<explode/>

transition/window_reenter

<?xml version="1.0" encoding="utf-8"?>
<explode/>

我正在使用普通的片段事务添加片段。我在像fragment.setEnterTransition() 这样的片段代码中不应用对转换API 的调用,因为我希望在给定styles.xml 中定义的转换的情况下自动应用转换。我不知道出了什么问题。 Android documentation 对片段转换并没有说太多。

【问题讨论】:

  • 为什么是 values-21/styles.xml,为什么不是 values/styles.xml?
  • @MadLeo 因为 API 21+ 支持转换 API。
  • 可能是因为你使用了支持片段?
  • 使用支持片段@Ufkoku有什么问题吗?
  • @codezombie 也许他们没有在 android: 命名空间中使用转换属性

标签: android android-fragments android-transitions


【解决方案1】:

我相信,您没有给出持续时间值。下面是一个示例幻灯片过渡

 <?xml version="1.0" encoding="utf-8"?>
 <transitionSet
 xmlns:android="http://schemas.android.com/apk/res/android">
     <Slide android:duration="10000">
        <targets>
            <target android:targetId="@+id/timer_clockView" />
            <target android:targetId="@+id/ic_wifi" />
            <target android:targetId="@+id/ic_lock" />
            <target android:targetId="@+id/ic_volume" />
        </targets>
     </Slide>
</transitionSet>

注意:确保您使用的是 android.support.v4.app.Fragment 而不是 android.app.Fragment,后者将在不久的将来被 Android 贬值。

【讨论】:

  • 没有。添加持续时间无效。
  • @codezombie 你在使用支持库和你的 API 版本吗?
  • 是的@Harish。支持库。实际上来自 androidx 包。最低 API 级别设置为 21。
  • @codezombie 转到您的片段 onResume() 方法,然后使用 getEnterTransition() 并调试它是否为非空值...如果是这种情况,那么您没有正确设置转换你的代码 sn-p 看起来不错。如果它是非空值并且它没有动画,那么它是片段相关库的未匹配..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-22
  • 2017-07-13
  • 1970-01-01
  • 1970-01-01
  • 2012-11-26
  • 2017-04-11
  • 1970-01-01
相关资源
最近更新 更多