【问题标题】:Android The background becomes transparent when the Fragment navigation animation startsAndroid Fragment导航动画启动时背景变为透明
【发布时间】:2021-03-08 16:13:26
【问题描述】:

片段

A -> B

A 动画正常

B 动画开始时View正常,但是背景是透明的

返回

A

A 动画正常

B 动画开始,背景消失,但View正常显示

如果将背景(android.R.color.white)设置为根视图

根视图的背景正常显示

代码

theme
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

navigation
<fragment
    android:id="@+id/mainFragment"
    android:name="MainFragment"
    android:label="MainFragment">

    <action
        android:id="@+id/action_to_setting"
        app:destination="@id/settingFragment"
        app:enterAnim="@anim/nav_enter"
        app:exitAnim="@anim/nav_exit"
        app:popEnterAnim="@anim/nav_pop_enter"
        app:popExitAnim="@anim/nav_pop_exit" />
</fragment>

nav_enter
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:shareInterpolator="true">
    <translate
        android:duration="300"
        android:fromXDelta="100%"
        android:toXDelta="0%" />
</set>

nav_exit
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:shareInterpolator="true">
    <translate
        android:duration="300"
        android:fromXDelta="0%"
        android:toXDelta="-30%" />
</set>

nav_pop_enter
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:shareInterpolator="true">
    <translate
        android:duration="300"
        android:fromXDelta="-30%"
        android:toXDelta="0%" />
</set>

nav_pop_exit
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:shareInterpolator="true">
    <translate
        android:duration="300"
        android:fromXDelta="0%"
        android:toXDelta="100%" />
</set>

基础片段

abstract class AppFragment(
    @LayoutRes var layoutId : Int,
    @LayoutRes var titleBarLayoutId : Int? = null,
    @StringRes var titleRes : Int? = null,
    var requestFocus : Boolean = true
) : Fragment(), AppLayoutInit {

    protected var root : View? = null

    override fun onCreateView(inflater : LayoutInflater, container : ViewGroup?, savedInstanceState : Bundle?) : View? {
        if (layoutId == AppLayoutInit.NULL_LAYOUT) return null
        root = if (titleBarLayoutId !== null) {
            createRootViewWithTitle(titleBarLayoutId!!, layoutId, inflater)
        } else {
            inflater.inflate(layoutId, container, false)
        }
        // If set, the background of the View will be displayed normally
        root!!.setBackgroundResource(android.R.color.white)
        return root
    }

    override fun onDestroyView() {
        root = null
        super.onDestroyView()
    }

    fun createRootViewWithTitle(
        @LayoutRes titleLayoutId : Int,
        @LayoutRes contentLayoutId : Int,
        inflater : LayoutInflater) : View {
        val root_ll = LinearLayout(inflater.context)
        root_ll.orientation = LinearLayout.VERTICAL
        val vlp = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        root_ll.layoutParams = vlp
        inflater.inflate(titleLayoutId, root_ll)
        val content = inflater.inflate(contentLayoutId, root_ll, false)
        val llp = content.layoutParams as LinearLayout.LayoutParams
        llp.height = 0
        llp.weight = 1f
        content.layoutParams = llp
        root_ll.addView(content)
        return root_ll
    }
}

versions.fragment = '1.2.5'

versions.navigation = '2.3.2'

【问题讨论】:

    标签: android android-fragments background navigation android-animation


    【解决方案1】:

    在我的情况下,“androidx.appcompat:appcompat:1.3.0”似乎导致了这个问题。降级到 1.2 解决了它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-23
      • 1970-01-01
      • 2023-03-03
      • 2021-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多