问题描述
android.support.v7.app.AppCompatActivity
昨天作为新功能添加到最新的 v7 支持库中.
android.support.v7.app.AppCompatActivity
was added into the latest v7 support library as a new feature yesterday.
据说 ActionBarActivity
已被弃用,取而代之的是新的 AppCompatActivity
并且 AppCompatActivity
是使用支持的活动的基类库操作栏功能.那么,AppCompatActivity
相对于 ActionBarActivity
有哪些新特性?AppCompatActivity
比 ActionBarActivity
有哪些改进?AppCompatActivity
的优点是什么?有人可以提供一些样品吗?
It is said that ActionBarActivity
has been deprecated in favor of the new AppCompatActivity
and that AppCompatActivity
is base class for activities that use the support library action bar features. So, what are new features of AppCompatActivity
over ActionBarActivity
? What enhancements do AppCompatActivity
have over ActionBarActivity
? And what are advantages of AppCompatActivity
? Could somebody supply a few samples?
PS:最让我惊讶的是,从 android.support.v4.app.FragmentActivity
扩展而来的 AppCompatActivity
是直接父ActionBarActivity
的类!我的意思是现在ActionBarActivity
可以做任何AppCompatActivity
可以做的事情,为什么Android推出了后者?
PS: what surprised me most is that AppCompatActivity
which is extended from android.support.v4.app.FragmentActivity
is the direct parent class of ActionBarActivity
! I mean actually now that ActionBarActivity
can do anything that AppCompatActivity
can do, why did Android pushed out the latter?
与此同时,我看到一篇博文指出:这不是从 ActionBarActivity
重命名为 AppCompatActivity
,AppCompat 可通过
"获得,那么内部逻辑"是什么?AppCompatDelegate
AppCompat
?AppCompatDelegate
能做什么?有人可以发布一些关于此的代码吗?
Meanwhile, I saw a blog post that states: "It's not a rename from ActionBarActivity
to AppCompatActivity
, the internal logic of AppCompat
is available via AppCompatDelegate
", so what's the "internal logic" of AppCompat
? What can AppCompatDelegate
do? Could somebody post some codes about this?
推荐答案
正如 Chris 所写,ActionBarActivity
的新弃用版本(扩展 AppCompatActivity
类)是一个安全的使用向后兼容类.它的弃用只是提示您要求直接使用新的 AppCompatActivity
.AppCompatActivity
是一个新的、更通用的实现,它在内部使用 AppCompatDelegate
类.
As Chris wrote, new deprecated version of ActionBarActivity
(the one extending AppCompatActivity
class) is a safe to use backward compatibility class. Its deprecation is just a hint for you asking to use new AppCompatActivity
directly instead. AppCompatActivity
is a new, more generic implementation which uses AppCompatDelegate
class internally.
如果您开始新的开发,那么您应该立即使用新的 AppCompatActivity
类.如果您有机会更新您的应用程序,则将已弃用的 ActionBarActivity
也替换为新的 Activity.否则,您可以继续使用已弃用的活动,而行为上根本没有区别.
If you start a new development, then you should rather use new AppCompatActivity
class right away. If you have a chance to update your app, then replace deprecated ActionBarActivity
by the new activity as well. Otherwise you can stay with deprecated activity and there will be no difference in behavior at all.
关于AppCompatDelegate
,它允许您在活动中拥有新的着色小部件,它既不是AppCompatActivity
,也不是ActionBarActivity
.
Regarding AppCompatDelegate
, it allows you to have new tinted widgets in an activity, which is neither AppCompatActivity
nor ActionBarActivity
.
例如,您从外部库继承了一个 Activity,而该外部库不从 AppCompatActivity
继承,但您希望此 Activity 具有着色材料小部件(意见).要实现它,您需要在您的活动中创建 AppCompatDelegate
的实例,覆盖该活动的方法,如 addContentView()
, setContentView()
等等(有关方法的完整列表,请参阅 AppCompatDelegate
javadoc),并且在这些重写的方法内部将调用转发到内部的 AppCompatDelegate
实例.AppCompatDelegate
将完成剩下的工作,您的老式"活动将具体化".
For instance, you inherit an activity from an external library, which, in turn, does not inherit from AppCompatActivity
but you want this activity to have tinted materials widgets (views). To make it happen you need to create an instance of AppCompatDelegate
inside your activity, override methods of that activity like addContentView()
, setContentView()
etc. (see AppCompatDelegate
javadoc for the full list of methods), and inside of those overridden methods forward the calls to the inner AppCompatDelegate
instance. AppCompatDelegate
will do the rest and your "old-fashion" activity will be "materialized".
这篇关于AppCompatActivity 相对于 ActionBarActivity 的增强是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!