全屏时 BottomNavigationView 跳起来留空 space
BottomNavigationView jumps up leaving blank space when fullscreen
对于BottomNavigationView的最后一个tab,我希望内容在状态栏下方,并且状态栏完全透明。这按预期工作,但 BottomNavigationView 跳起来,在选择最后一个选项卡时在底部留下空白 space。
SO 中有类似的主题说要将 fitWindowSystems 设置为 false。我试过了,但没有用。
ExplorerFragment.java
public class ExplorerFragment extends Fragment {
public ExplorerFragment() {
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Window window = getActivity().getWindow();
window.getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE);
window.setStatusBarColor(ContextCompat.getColor(getContext(), android.R.color.transparent));
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
View decor = getActivity().getWindow().getDecorView();
decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
}
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_explorer, container, false);
}
}
fragment_explorer.xml
<android.support.constraint.ConstraintLayout
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" xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.orevon.fflok.fragments.ExplorerFragment"
android:fitsSystemWindows="false">
<View android:layout_width="0dp" android:layout_height="180dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:background="@drawable/splash_background4"/>
</android.support.constraint.ConstraintLayout>
activity_mail.xml
<?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"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false">
<FrameLayout
android:id="@+id/fragment_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/mainBottomNavigation"
android:layout_width="match_parent"
android:layout_height="68dp"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:background="@drawable/bottombar_bg"
app:itemIconTint="@color/bottom_nav_color"
app:itemTextColor="@color/bottom_nav_color"
app:menu="@menu/main_bottom_navigation"/>
</android.support.constraint.ConstraintLayout>
可能对以后遇到问题的人有用。
我通过添加
解决了这个问题
ViewCompat.requestApplyInsets(view)
到我片段的 onActivityCreated
方法。
view
- 片段的根视图
在我的例子中它看起来像:
KOTLIN
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
view?.let {
ViewCompat.requestApplyInsets(it)
}
}
JAVA
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
View view = getView()
if(view != null){
ViewCompat.requestApplyInsets(view)
}
}
更新(6/27/2020):
由于 onActivityCreated
方法在 SDK >=29 上被弃用,您可以在 onViewCreated
方法中使用上面的代码:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
ViewCompat.requestApplyInsets(view)
}
问题的解决方法是将bottomNavigationView setOnApplyWindowsListener设置为null:
setOnApplyWindowInsetsListener(null)
我在为我的 AR Core Sceneform 片段实现底部导航视图时遇到了这个问题。
Extra padding issue
Extra padding issue resolved
对于BottomNavigationView的最后一个tab,我希望内容在状态栏下方,并且状态栏完全透明。这按预期工作,但 BottomNavigationView 跳起来,在选择最后一个选项卡时在底部留下空白 space。
SO 中有类似的主题说要将 fitWindowSystems 设置为 false。我试过了,但没有用。
ExplorerFragment.java
public class ExplorerFragment extends Fragment {
public ExplorerFragment() {
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Window window = getActivity().getWindow();
window.getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE);
window.setStatusBarColor(ContextCompat.getColor(getContext(), android.R.color.transparent));
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
View decor = getActivity().getWindow().getDecorView();
decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
}
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_explorer, container, false);
}
}
fragment_explorer.xml
<android.support.constraint.ConstraintLayout
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" xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.orevon.fflok.fragments.ExplorerFragment"
android:fitsSystemWindows="false">
<View android:layout_width="0dp" android:layout_height="180dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:background="@drawable/splash_background4"/>
</android.support.constraint.ConstraintLayout>
activity_mail.xml
<?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"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false">
<FrameLayout
android:id="@+id/fragment_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/mainBottomNavigation"
android:layout_width="match_parent"
android:layout_height="68dp"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:background="@drawable/bottombar_bg"
app:itemIconTint="@color/bottom_nav_color"
app:itemTextColor="@color/bottom_nav_color"
app:menu="@menu/main_bottom_navigation"/>
</android.support.constraint.ConstraintLayout>
可能对以后遇到问题的人有用。 我通过添加
解决了这个问题ViewCompat.requestApplyInsets(view)
到我片段的 onActivityCreated
方法。
view
- 片段的根视图
在我的例子中它看起来像:
KOTLIN
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
view?.let {
ViewCompat.requestApplyInsets(it)
}
}
JAVA
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
View view = getView()
if(view != null){
ViewCompat.requestApplyInsets(view)
}
}
更新(6/27/2020):
由于 onActivityCreated
方法在 SDK >=29 上被弃用,您可以在 onViewCreated
方法中使用上面的代码:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
ViewCompat.requestApplyInsets(view)
}
问题的解决方法是将bottomNavigationView setOnApplyWindowsListener设置为null:
setOnApplyWindowInsetsListener(null)
我在为我的 AR Core Sceneform 片段实现底部导航视图时遇到了这个问题。
Extra padding issue
Extra padding issue resolved