Android studio的工程默認繼承AppcompatActivity類和Activity不同?

最近在學習安卓,使用Android Studio在選擇最小sdk Android 4.0的時候,自動生成的工程中默認繼承的是AppCompatActivitu類,但是我看的教學視頻是用Eclipse操作的,它生成的工程是繼承自Activity,剛開始的時候並沒有發現什麼,但是學到ActionBar出了問題,我的工程them是appthem這個style,繼承自them.appcompat.light但是視頻上說安卓3.0以上的主題是holo,我把主題換成holo程序就報錯,提示不能用這個主題,而且有些操作我也無法完成,比如getActiobBar這個方法,我用就會報錯,提示 null project 而且Bar上也添加不了圖標,比如分享圖標,在menu.xml文件中定義了item也顯示不了,也沒有setSherIntent方法,還有用自動生成ActionBar網站生成的格式也無法應用,百度看了也沒有說這個問題的,不知道該怎麼解決。。。。

我看到 AppCompat最後也是繼承Activity的,為什麼會出現這樣的問題呢


Activity 發展到3.0(大概)之後,可以使用fragment了,但是support v4 提供了1.6~3.0的fragment兼容,所以如果需要用兼容版的fragment,則需要繼承support v4提供的FragmentActivity。而後一點點時間之後,3.0(大概)出現的ActionBar也被向前支持了,這次是出現在support v7里,如果需要使用兼容版的actionbar,則繼承support v7提供的ActionBarActivity(它是繼承FragmentActivity的)。再然後也就是去年年底到今年,5.0提供了很多很多新東西,於是support v7也更新了,出現了AppCompatActivity , 具體功能請自行查找。

https://blog.xamarin.com/android-tips-hello-appcompatactivity-goodbye-actionbaractivity/

還有同學,你的信息獲取渠道需要更新了,ActionBar已經被划上了橫線,取而代之的是ToolBar,學習編程最忌諱的就是因循守舊,技術革新這麼快,說不定2/3都不適用現在了,而且百度隨便能搜到的教程大多都是觀點偏頗角度單一用法錯誤解釋不到位等等。所以帶上一些工程師的熱情,備好梯子和詞典,主動去廣泛獲取知識。


Activity is the baseline. Every activity inherits from Activity, directly or indirectly.

FragmentActivity is for use with the backport of fragments found in the support-v4 and support-v13 libraries. The native implementation of fragments was added in API Level 11, which is higher than your proposed minSdkVersion values. The only reason why you would need to consider FragmentActivity specifically is if you want to use nested fragments (a fragment holding another fragment), as that was not supported in native fragments until API Level 17.

AppCompatActivity is from the appcompat-v7 library. Principally, this offers a backport of the action bar. Since the native action bar was added in API Level 11, you do not need AppCompatActivity for that. However, current versions of appcompat-v7 also add a limited backport of the Material Design aesthetic, in terms of the action bar and various widgets. There are pros and cons of using appcompat-v7, well beyond the scope of this specific Stack Overflow answer.

ActionBarActivity is the old name of the base activity from appcompat-v7. For various reasons, they wanted to change the name. Unless some third-party library you are using insists upon an ActionBarActivity, you should prefer AppCompatActivity over ActionBarActivity.

So, given your minSdkVersion in the 15-16 range:

  • If you want the backported Material Design look, use AppCompatActivity

  • If not, but you want nested fragments, use FragmentActivity

  • If not, use Activity


點進去看一下不就知道了嗎?


推薦閱讀:

2016 年第一季度,Android 有什麼讓人興奮的新技術出現嗎?
如何評價OPPO R9S升級後標識錯誤的Android版本號事件?
如何評價 Android M?
iOS 的應用質量具體在哪些方面高於安卓應用?
為什麼安卓手機會越用越卡,有讓它變快的方法嗎?

TAG:編程 | Android開發 | Android |