简体   繁体   中英

Add custom layout to standard Toolbar

I want to add my custom layout to the side of the existing Toolbar, keeping all features with NavigationUI (Up button, animations, placing up buttons and drawers, etc)

How can i achieve this?

After some research i`ve found out the solution


MainActivity.kt

class MainActivity : AppCompatActivity() {

    private val navController by lazy { findNavController(R.id.main_nav_host_fragment) }
    private val appBarConfiguration by lazy { AppBarConfiguration(navController.graph) }

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val navController = this.findNavController(R.id.main_nav_host_fragment)
        NavigationUI.setupActionBarWithNavController(this, navController)
        NavigationUI.setupWithNavController(
            findViewById<NavigationView>(R.id.navigation_view),
            navController
        )
    }

    override fun onSupportNavigateUp(): Boolean {
        val navController = this.findNavController(R.id.main_nav_host_fragment)
        return NavigationUI.navigateUp(navController, appBarConfiguration)
    }
}


FragmentToolbar.kt interface

interface FragmentToolbar {
    fun clearActionBar()
    fun setCustomActionBar()
}


MainFragment.kt
Next class MainFragment.kt is abstract with methods clearActionBar() and setCustomActionBar() that can be overridden. They are defined in FragmentToolbar.kt interface, because if you set custom layout in first fragment, you will see it also in all other fragments. So, you will almost always have to clear your ActionBar and this class is responsible for standard realisation. setCustomActionBar() is up to you\

abstract class MainFragment : Fragment(), FragmentToolbar {

val actionBar: ActionBar? by lazy { (requireActivity() as AppCompatActivity).supportActionBar }

override fun clearActionBar() {
    actionBar?.apply {
        setDisplayShowCustomEnabled(false)
        setDisplayShowTitleEnabled(true)
    }
}

override fun setCustomActionBar() {}

}


MyFragment.kt

class MyFragment : MainFragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {

        clearActionBar() // defined in MainFragment

        // Your code
    }

    override fun setCustomActionBar() {
        actionBar?.apply {
            setDisplayShowCustomEnabled(true)
            setCustomView(R.layout.view_cart_and_bill)
        }

        actionBar?.customView?.apply { /* Here use findViewById to find necessary views inside your custom layout */ }
    }
}

Hope my answer helps you. It also would be great, if ViewBinding could be used with this layout added in runtime, but i have not solved this issue yet

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