我来自iOS,它很简单,你只需要使用UIViewController。然而,在Android中,事情似乎要复杂得多,特定的API级别有特定的uiccomponent。我正在阅读BigNerdRanch for Android(这本书大约有2年的历史了),他们建议我使用Activity来托管我的FragmentActivities。然而,我认为活动是不赞成的。
那么对于API级别22(至少支持API级别15或16),我究竟应该使用什么来托管组件,以及组件本身呢?所有这些都有用处吗,还是我应该只使用其中一两个?
2019:使用AppCompatActivity
在写这篇文章的时候(检查链接确认它仍然是正确的),Android文档建议使用AppCompatActivity如果你正在使用应用程序栏。
这是给定的理性:
Beginning with Android 3.0 (API level 11), all activities that use
the default theme have an ActionBar as an app bar. However, app bar
features have gradually been added to the native ActionBar over
various Android releases. As a result, the native ActionBar behaves
differently depending on what version of the Android system a device
may be using. By contrast, the most recent features are added to the
support library's version of Toolbar, and they are available on any
device that can use the support library.
For this reason, you should use the support library's Toolbar class to
implement your activities' app bars. Using the support library's
toolbar helps ensure that your app will have consistent behavior
across the widest range of devices. For example, the Toolbar widget
provides a material design experience on devices running Android 2.1
(API level 7) or later, but the native action bar doesn't support
material design unless the device is running Android 5.0 (API level
21) or later.
添加工具栏的一般方向是
添加v7版appcompat支持库
让你所有的活动扩展AppCompatActivity
在Manifest中声明你想要NoActionBar。
在每个活动的xml布局中添加一个工具栏。
在每个活动的onCreate中获取工具栏。
有关详细信息,请参阅文档说明。它们很清楚,很有帮助。
Activity是所有其他Activity的基类,我不认为它会被弃用。它们之间的关系是:
Activity <- FragmentActivity <- AppCompatActivity <- ActionBarActivity
'<-'在这里表示继承。参考说ActionBarActivity已弃用,改用AppCompatActivity。
所以基本上,使用AppCompatActivity总是正确的选择。它们之间的区别是:
活动是最基本的。
基于Activity, FragmentActivity提供了使用Fragment的能力。
基于FragmentActivity, AppCompatActivity为ActionBar提供了一些特性。
2019:使用AppCompatActivity
在写这篇文章的时候(检查链接确认它仍然是正确的),Android文档建议使用AppCompatActivity如果你正在使用应用程序栏。
这是给定的理性:
Beginning with Android 3.0 (API level 11), all activities that use
the default theme have an ActionBar as an app bar. However, app bar
features have gradually been added to the native ActionBar over
various Android releases. As a result, the native ActionBar behaves
differently depending on what version of the Android system a device
may be using. By contrast, the most recent features are added to the
support library's version of Toolbar, and they are available on any
device that can use the support library.
For this reason, you should use the support library's Toolbar class to
implement your activities' app bars. Using the support library's
toolbar helps ensure that your app will have consistent behavior
across the widest range of devices. For example, the Toolbar widget
provides a material design experience on devices running Android 2.1
(API level 7) or later, but the native action bar doesn't support
material design unless the device is running Android 5.0 (API level
21) or later.
添加工具栏的一般方向是
添加v7版appcompat支持库
让你所有的活动扩展AppCompatActivity
在Manifest中声明你想要NoActionBar。
在每个活动的xml布局中添加一个工具栏。
在每个活动的onCreate中获取工具栏。
有关详细信息,请参阅文档说明。它们很清楚,很有帮助。