简体   繁体   中英

Toolbar not showing up in Fragment

I've created a Toolbar and added it on my main_activity.xml as follows:

<?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>

Also changed the style.xml to : <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

And in MainActivity I've added this :

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

But even with this is not showing on the first Fragment

I do a fragment.replace() to show the first Fragment , and I do not know if that's the wrong thing

The FrameLayout in which the fragment loaded is on top of the ToolBar. That's why you can not see it. Add android:layout_marginTop="?android:attr/actionBarSize" to the framelayout.

Adding the following code to Framelayout will solve the problem.

android:layout_below="@id/toolbar_detail"

I hope I could help.

In my case, the toolbar was not hidden, it just wasnt there. I was able to have it on the fragment by removing it from the activity and putting it on the fragment layout and controlling it from there. Like this:

on the layout:

 <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/funkey_orange" />

</com.google.android.material.appbar.AppBarLayout>

on the fragment kotlin after OnCreateView:

 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        var myToolbar = requireActivity()
            .findViewById<androidx.appcompat.widget.Toolbar>(R.id.toolbar)
        myToolbar.inflateMenu(R.menu.menu)

} 

after this I put the options menu:

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        inflater.inflate(R.menu.menu, menu)
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM