【问题标题】:Why the toolbar is being ignored after I switch a fragment in Android?为什么我在 Android 中切换片段后工具栏被忽略?
【发布时间】:2019-06-22 12:20:18
【问题描述】:

我正在构建一个带有BottomNavigationView 的应用程序,其中第一个选项卡已正确加载:

标签 1:

但是,当我切换到下一个选项卡时,框架布局似乎像这样位于工具栏下方:

标签 2:

再次进入 Tab 1:

这是我的布局:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:layout_above="@+id/bottom_navigation"/>
    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:menu="@menu/bottom_nav_menu"
        app:labelVisibilityMode="unlabeled"
        app:itemBackground="@color/colorPrimary"
        app:itemIconTint="@color/bottom_nav_color"
        app:itemTextColor="@color/bottom_nav_color" />
</RelativeLayout>

类似这样的案例还有:

Part of Fragment items hides under Action bar

但他们的解决方案在我的情况下不起作用。知道我应该改变什么吗?或者为什么工具栏失去了优先权?我已经在 Layout 中测试了自己创建一个,但它也不能正常工作。

附言

我正在加载一些 WebViews,但我怀疑它会影响。

【问题讨论】:

    标签: android android-layout android-relativelayout android-framelayout bottomnavigationview


    【解决方案1】:

    您的代码对我来说运行良好。虽然您可以为框架布局设置边距顶部

    android:layout_marginTop="?attr/actionBarSize"

    【讨论】:

    • 你认为它会影响我在每个片段中只有 WebViews 吗?
    • 如果你设置了你的代码,那么第一次出现无缘无故的空格:/
    • 我不认为 webview 会产生任何影响,但也许你在代码中做了一些事情正在这样做。此外,您可以使用 NoActionBar 主题并在布局中添加工具栏。此外,您可以创建一个新的底部导航活动并检查它是否解决了您的问题。
    【解决方案2】:

    我找到了如何使用 CoordinatorLayout 修复它:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <android.support.design.widget.CoordinatorLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"            
                android:layout_above="@+id/bottom_navigation">
                <FrameLayout
                    android:id="@+id/content_frame"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            </android.support.design.widget.CoordinatorLayout>
            <android.support.design.widget.BottomNavigationView
                android:id="@+id/bottom_navigation"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                app:menu="@menu/bottom_nav_menu"
                app:labelVisibilityMode="unlabeled"
                app:itemBackground="@color/colorPrimary"
                app:itemIconTint="@color/bottom_nav_color"
                app:itemTextColor="@color/bottom_nav_color" />
        </RelativeLayout>
    </android.support.v4.widget.DrawerLayout>
    

    我从这里得到了一个想法:

    https://forums.xamarin.com/discussion/146285/how-to-do-appbarlayout-with-collapsing-toolbarlayout-that-will-collapse-when-viewpager-scroll

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-18
      相关资源
      最近更新 更多