android - 当页面之一包含折叠工具栏并且 fitSystemWindows 为真时,Android BottomNavigationBar 移动
问题描述
我有一个使用 BottomNavigationView 的活动。在导航中,我显示/隐藏两个片段,其中一个是空片段,另一个包含已设置的折叠工具栏android:fitsSystemWindows="true"
。
问题是 - 当我打开折叠片段选项卡并导航回空片段时,底部导航视图向下移动(超出设备屏幕)。
当我没有将 fitSystemWindows 设置为 true 或每次用户更改选项卡时创建新片段时,问题不会出现。但我需要保留两者。
有没有办法摆脱这种情况?
这是示例代码,其行为如此
活动
class BottomNavigationActivity : AppCompatActivity() {
private var currentFragment: Fragment? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_bottom_navigation)
val bottomNavigationView = findViewById(R.id.activity_navigation__bottom_bar) as BottomNavigationView
bottomNavigationView.setOnNavigationItemSelectedListener {
when (it.itemId) {
R.id.action_blank -> openFragment(findFragment("blank")?:BlankFragment(), "blank")
R.id.action_collapsed -> openFragment(findFragment("collapsing")?:CollapsingFragment(), "collapsing")
}
return@setOnNavigationItemSelectedListener true
}
}
private fun openFragment(fragment: Fragment, tag: String) {
val fragmentTransaction = supportFragmentManager.beginTransaction()
currentFragment?.let {
fragmentTransaction.hide(it)
}
if (!fragment.isAdded) {
fragmentTransaction.add(R.id.container, fragment, tag);
}
if (!fragment.isVisible) {
fragmentTransaction.show(fragment)
}
currentFragment = fragment
fragmentTransaction.commitAllowingStateLoss()
}
private fun findFragment(tag: String): Fragment? {
return supportFragmentManager.findFragmentByTag(tag)
}
}
活动布局xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="@id/activity_navigation__bottom_bar"/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/activity_navigation__bottom_bar"
android:background="@color/colorPrimary"
android:layout_width="match_parent"
app:menu="@menu/menu"
android:layout_height="56dp"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
空片段
class BlankFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_blank, container, false)
}
}
空片段布局xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BlankFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="blank fragment"/>
</FrameLayout>
崩溃的片段
class CollapsingFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_collapsing, container, false)
}
}
折叠片段布局xml
<?xml version="1.0" encoding="utf-8"?>
<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:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/fragment_collapsing_toolbar__appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/fragment_collapsing_toolbar__container"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="?android:attr/windowBackground"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
android:id="@+id/fragment_collapsing_toolbar__toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="collapsing fragment"
app:titleMarginStart="72dp"
app:titleMarginEnd="72dp"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/fragment_collapsing_toolbar__content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.design.widget.CoordinatorLayout>
这是菜单
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_blank"
android:enabled="true"
android:title="blank"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_collapsed"
android:enabled="true"
android:title="collapsing"
app:showAsAction="ifRoom" />
</menu>
谢谢。
解决方案
推荐阅读
- angular - 没有“数据”要求的路由转换?
- react-select - 如何为多选设置默认值?
- android - 查找没有 GPS 的 3G 路由器的位置
- c++ - 内存交换或文件写入/读取
- sql - TSQL - 过滤组,如果它包含所有空
- android - RxJava2 与 Kotlin 协程
- redhat-containers - HYPERV_VIRTUAL_SWITCH 已弃用,用什么代替?
- javascript - Javascript 计时器在点击开始之前已经运行
- php - 如何在mysql中使用纽约时区获取最近12个月的数据?
- function - 具有 span 函数的 Haskell 组定义