活动如何使用工具栏不延长AppCompatActivity [英] How can an activity use a Toolbar without extending AppCompatActivity
问题描述
我有一个活动 HomeView
这已经扩展了另一个活动,并不能延长 AppCompatActivity
。但HomeView需要有一个工具栏。 Android的文件说,这需要有一个工具栏的任何活动都必须扩展 AppCompatActivity
。
I have an activity HomeView
which already extends another activity and it cannot extend AppCompatActivity
. But HomeView needs to have a Toolbar. The Android documentation says that any activity which needs to have a Toolbar must extend AppCompatActivity
.
如何才能绕过这个限制?
How can I get around this limitation?
推荐答案
您需要执行 AppCompatCallback
,并使用 AppCompatDelegate
。下面是关于如何使用它的优秀文章:<一href=\"https://medium.com/google-developer-experts/how-to-add-toolbar-to-an-activity-which-doesn-t-extend-appcompatactivity-a07c026717b3#.nuyghrgr9\" rel=\"nofollow\">https://medium.com/google-developer-experts/how-to-add-toolbar-to-an-activity-which-doesn-t-extend-appcompatactivity-a07c026717b3#.nuyghrgr9并检查出的https://developer.android.com/reference/android/support/v7/app/AppCompatDelegate.html为了解委托哪些方法。
You need to implement AppCompatCallback
and use AppCompatDelegate
. Here's an excellent article about 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 also check out https://developer.android.com/reference/android/support/v7/app/AppCompatDelegate.html for knowing which methods to delegate.
本类重presents一个委托,您可以使用应用程序兼容性的支持扩展到任何活动。
This class represents a delegate which you can use to extend AppCompat's support to any Activity.
当使用AppCompatDelegate,你应该任何方法暴露了它,而不是相同名称的活动方式。适用于:
When using an AppCompatDelegate, you should any methods exposed in it rather than the Activity method of 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 also some Activity lifecycle methods which should be proxied to the delegate:
onCreate(android.os.Bundle)
onPostCreate(android.os.Bundle)
onConfigurationChanged(android.content.res.Configuration)
setTitle(CharSequence)
onStop()
onDestroy()
这篇关于活动如何使用工具栏不延长AppCompatActivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!