Activity 从 Fragment 更新时布局跳到状态栏后面
Activity layout jumping behind statusbar, when updated from Fragment
问题
我有一个 activity,我的用户可以在其中编辑实体的不同值。我创建了一个 BottomSheetDialogFragment,用户可以在其中更改所述实体的一些时间值。
当用户更改值时(这里是时间),我也想更改 activity 在后台显示的值。然而,当我更新我的 activity 视图时,整个 activity 像 this 一样跳到状态栏后面(我知道选择的数字和显示的数字之间存在不一致,这是时区问题我还没有修好。
代码
我用以下命令打开 BottomSheetDialogFragment:
TimePickerDialogFrag.newInstance(shift!!, type).run {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
show(supportFragmentManager, "timepicker_modal")
}
BottomSheetDialogFragment 通过委托模式与 activity 通信,委托模式由名为 OnTimeChangedListener 的接口定义,如下所示:
override fun onAttach(context: Context) {
super.onAttach(context)
if (context is OnTimeChangedListener) {
listener = context
} else {
throw RuntimeException(context.toString() + " must implement OnTimeChangedListener")
}
}
override fun onDetach() {
super.onDetach()
listener = null
}
interface OnTimeChangedListener {
fun onTimeChanged(shift: ShiftEntity)
}
当用户旋转 TimePicker 小部件时调用 onTimeChanged 方法。
方法在activity中实现为:
override fun onTimeChanged(shift: ShiftEntity) {
this.shift = shift
startTimeTextView?.text = TimeFormatter.formatTime(shift.startTime, "HH:mm")
endTimeTextView?.text = TimeFormatter.formatTime(shift.endTime, "HH:mm")
}
我猜解决方案在 Activity 布局文件中的某处,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
android:focusableInTouchMode="true"
tools:context=".activities.myshifts.ShiftActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
style="@style/myNavDrawer"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="@color/primaryBg"
android:contentInsetEnd="0dp"
android:contentInsetLeft="0dp"
android:contentInsetRight="0dp"
android:contentInsetStart="0dp"
app:contentInsetEnd="0dp"
app:contentInsetEndWithActions="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
app:popupTheme="@style/AppTheme"
app:theme="@style/ToolbarColoredBackArrow">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
//Content of custom AppBar goes here. Removed for an easier overview
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
//Actual content of activity goes here. Removed it for an easier
overview, will add if needed.
</android.support.constraint.ConstraintLayout>
已经尝试过
我已经尝试使用 android:fitsSystemWindows="true" 属性 - 将其添加到父视图和 AppBarLayout。
将其添加到父布局会在 Fragment 打开后立即在 AppBar 内创建填充,如顶部所示 here
将属性添加到 AppBarLayout 并没有什么不同。
尝试添加此属性:
android:fitsSystemWindows="true"
在您膨胀的 xml 的 BottomSheetDialogFragment
根中,或在包含您的 Fragment
.
的 FrameLayout
中
我找到了解决方案。我没有意识到我膨胀 BottomSheetDialogFragment 的方式会产生影响,因此我最初没有在问题中包含该代码。
但是从片段中删除 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
就可以了。
问题
我有一个 activity,我的用户可以在其中编辑实体的不同值。我创建了一个 BottomSheetDialogFragment,用户可以在其中更改所述实体的一些时间值。
当用户更改值时(这里是时间),我也想更改 activity 在后台显示的值。然而,当我更新我的 activity 视图时,整个 activity 像 this 一样跳到状态栏后面(我知道选择的数字和显示的数字之间存在不一致,这是时区问题我还没有修好。
代码
我用以下命令打开 BottomSheetDialogFragment:
TimePickerDialogFrag.newInstance(shift!!, type).run {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
show(supportFragmentManager, "timepicker_modal")
}
BottomSheetDialogFragment 通过委托模式与 activity 通信,委托模式由名为 OnTimeChangedListener 的接口定义,如下所示:
override fun onAttach(context: Context) {
super.onAttach(context)
if (context is OnTimeChangedListener) {
listener = context
} else {
throw RuntimeException(context.toString() + " must implement OnTimeChangedListener")
}
}
override fun onDetach() {
super.onDetach()
listener = null
}
interface OnTimeChangedListener {
fun onTimeChanged(shift: ShiftEntity)
}
当用户旋转 TimePicker 小部件时调用 onTimeChanged 方法。
方法在activity中实现为:
override fun onTimeChanged(shift: ShiftEntity) {
this.shift = shift
startTimeTextView?.text = TimeFormatter.formatTime(shift.startTime, "HH:mm")
endTimeTextView?.text = TimeFormatter.formatTime(shift.endTime, "HH:mm")
}
我猜解决方案在 Activity 布局文件中的某处,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
android:focusableInTouchMode="true"
tools:context=".activities.myshifts.ShiftActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
style="@style/myNavDrawer"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="@color/primaryBg"
android:contentInsetEnd="0dp"
android:contentInsetLeft="0dp"
android:contentInsetRight="0dp"
android:contentInsetStart="0dp"
app:contentInsetEnd="0dp"
app:contentInsetEndWithActions="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
app:popupTheme="@style/AppTheme"
app:theme="@style/ToolbarColoredBackArrow">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
//Content of custom AppBar goes here. Removed for an easier overview
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
//Actual content of activity goes here. Removed it for an easier
overview, will add if needed.
</android.support.constraint.ConstraintLayout>
已经尝试过
我已经尝试使用 android:fitsSystemWindows="true" 属性 - 将其添加到父视图和 AppBarLayout。
将其添加到父布局会在 Fragment 打开后立即在 AppBar 内创建填充,如顶部所示 here
将属性添加到 AppBarLayout 并没有什么不同。
尝试添加此属性:
android:fitsSystemWindows="true"
在您膨胀的 xml 的 BottomSheetDialogFragment
根中,或在包含您的 Fragment
.
FrameLayout
中
我找到了解决方案。我没有意识到我膨胀 BottomSheetDialogFragment 的方式会产生影响,因此我最初没有在问题中包含该代码。
但是从片段中删除 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
就可以了。