【发布时间】: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