代码之家  ›  专栏  ›  技术社区  ›  StuartDTO

工具栏未在片段中显示

  •  0
  • StuartDTO  · 技术社区  · 6 年前

    我创造了一个 Toolbar 并将其添加到my main_activity.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"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_detail"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            android:background="@color/loginBrown"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            app:contentInsetStart="0dp"
            app:contentInsetStartWithNavigation="0dp">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="horizontal">
    
    
                <TextView
                    android:id="@+id/toolbar_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center_horizontal"
                    android:textColor="#fff"
                    android:textSize="16sp" />
            </LinearLayout>
        </android.support.v7.widget.Toolbar>
    
        <FrameLayout
            android:id="@+id/frame_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/navigation"
            android:animateLayoutChanges="true">
        </FrameLayout>
        <android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@color/colorPrimary"
            app:itemIconTint="@color/nav_item_state_list"
            app:itemTextColor="@color/nav_item_state_list"
            app:menu="@menu/bottom_navigation_items" />
    </RelativeLayout>
    

    也改变了 style.xml 到: <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

    而在 MainActivity 我添加了以下内容:

    Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar_detail);
    setSupportActionBar(toolbar);
    

    但即使这样也不会在第一次 Fragment

    我做 fragment.replace() 显示第一个 破片 ,我不知道那是不是错的

    2 回复  |  直到 6 年前
        1
  •  2
  •   Bertram Gilfoyle    6 年前

    加载片段的framelayout位于工具栏顶部。这就是为什么你看不见它。添加 android:layout_marginTop="?android:attr/actionBarSize" 到framelayout。

        2
  •  0
  •   fevziomurtekin    6 年前

    将以下代码添加到framelayout将解决此问题。

    android:layout_below="@id/toolbar_detail"
    

    我希望我能帮忙。