Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

android - Support Action Bar visible only in one fragment

My app consist of MainActivity and 2 fragments. MainActivity works as Navigation Host.

In my app I've disabled global ActionBar, instead I set my own in MainFragment

    val toolbar: Toolbar = v.findViewById(R.id.toolbar)
    (activity as AppCompatActivity).setSupportActionBar(toolbar)

Toolbar is also included in XML file for this fragment:

<include
    android:id="@+id/include"
    layout="@layout/toolbar_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

The problem's when I go to other fragment's screen Toolbar isn't showing.

MainActivity XML

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:fitsSystemWindows="true"
    tools:context=".MainActivity"
    android:orientation="vertical">

    
    <fragment
        android:id="@+id/fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph" />
    
</LinearLayout>

Where is the problem?

question from:https://stackoverflow.com/questions/65939756/support-action-bar-visible-only-in-one-fragment

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The action bar is visible in only one fragment because I am assuming you inflate it only in one fragment. If you wish to have a custom toolbar inflate once and appear in all fragments, then inflate the toolbar in the base activity

So try the following,

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:fitsSystemWindows="true"
    tools:context=".MainActivity"
    android:orientation="vertical">

<!-- Add your custom toobar here -->
    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"/>

    <fragment
        android:id="@+id/fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph" />
    
</LinearLayout>

MainActivity.kt

private lateinit var toolbar: Toolbar

override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  setContentView(R.layout.activity_main)

  toolbar = findViewById<Toolbar>(R.id.toolbar)
  setSupportActionBar(toolbar)

 // rest of your logic
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...