Android activities how to use toolbars without extending appcompatactivity

I have an activity, homeview, which has extended another activity. It cannot extend appcompatactivity. But homeview needs a toolbar. The Android document says that any activity that needs a toolbar must extend appcompatactivity

How do I address this limitation?

resolvent:

You need to implement appcompatcallback and use appcompatdelegate. This is an excellent article on how to use it: https://medium.com/google-developer-experts/how-to-add-toolbar-to-an-activity-which-doesn-t-extend-appcompatactivity-a07c026717b3#.nuyghrgr9 And view https://developer.android.com/reference/android/support/v7/app/AppCompatDelegate.html with Know which method to delegate

AppCompatDelegate

This class represents a delegate that you can use to extend appcompat support to any activity

When using appcompartdelegate, you should use any method disclosed therein, instead of the activity method with the same name. This applies to:

addContentView(android.view.View, android.view.ViewGroup.LayoutParams)
setContentView(int)
setContentView(android.view.View)
setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
requestWindowFeature(int)
invalidateOptionsMenu()
startSupportActionMode(android.support.v7.view.ActionMode.Callback)
setSupportActionBar(android.support.v7.widget.Toolbar)
getSupportActionBar()
getMenuInflater()

There are also some activity lifecycle methods that should be delegated to agents:

onCreate(android.os.Bundle)
onPostCreate(android.os.Bundle)
onConfigurationChanged(android.content.res.Configuration)
setTitle(CharSequence)
onStop()
onDestroy()

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>