活动,AppCompatActivity,FragmentActivity和ActionBarActivity:什么时候使用? [英] Activity, AppCompatActivity, FragmentActivity, and ActionBarActivity: When to Use Which?

本文介绍了活动,AppCompatActivity,FragmentActivity和ActionBarActivity:什么时候使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从未来的iOS它很容易与您只需使用一个UIViewController。然而,在Android的东西似乎要复杂得多,与特定的API级别一定的UIComponents。我读BigNerdRanch为Android(书大概为2岁),他们建议我使用活动主办我的 FragmentActivities 。不过,我以为活动是德precated。

I'm coming from iOS where it's easy and you simply use a UIViewController. However, in Android things seem much more complicated, with certain UIComponents for specific API Levels. I'm reading BigNerdRanch for Android (the book is roughly 2 years old) and they suggest I use Activity to host my FragmentActivities. However, I thought Activity was deprecated.

因此​​,对于API等级22(与API级别15或16的最低支持),正是我应该使用这两种主办的组件以及组件本身?对于所有这些有使用,或者我应该使用一个或两个几乎完全?

So for API Level 22 (with a minimum support for API Level 15 or 16), what exactly should I use both to host the components, and for the components themselves? Are there uses for all of these, or should I be using one or two almost exclusively?

感谢您。

推荐答案

有关的15最低API级别,您想使用AppCompatActivity。因此,例如,您MainActivity应该是这样的:

For a minimum API level of 15, you'd want to use AppCompatActivity. So for example, your MainActivity would look like this:

public class MainActivity extends AppCompatActivity {
    ....
    ....
}

要使用AppCompatActivity,请确保您有谷歌支持库下载的(你可以在你的工具检查这 - > Android的 - > SDK管理器)。然后,只需在您的应用程序的gradle.build文件依赖的gradle:

To use the AppCompatActivity, make sure you have the Google Support Library downloaded (you can check this in your Tools -> Android -> SDK manager). Then just include the gradle dependency in your app's gradle.build file:

compile 'com.android.support:appcompat-v7:22:2.0'

您可以使用此应用程序兼容性作为您的主要活动,然后可以用于启动片段或其他活动(这取决于你正在构建什么样的应用程序)。

You can use this AppCompat as your main Activity, which can then be used to launch Fragments or other Activities (this depends on what kind of app you're building).

该BigNerdRanch本书是一个很好的资源,但是,是的,这是过时的。读它在Android上如何工作的一般信息,但不要指望他们使用是最新的具体类。

The BigNerdRanch book is a good resource, but yeah, it's outdated. Read it for general information on how Android works, but don't expect the specific classes they use to be up to date.

这篇关于活动,AppCompatActivity,FragmentActivity和ActionBarActivity:什么时候使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆