Android ActionBar上不顯示icon的問題
阿新 • • 發佈:2019-01-02
又是一個 support library的問題,最近被這個android.support.v4 和android.support.v7 折磨的要死要活的,很多東西因為版本的變動有好多的不同。由於我的專案用了 Fragment的v4 surpport包,所以在Activity繼承ActionBarActivity的時候繼承了v7的,不知道為什麼操作欄上Item的圖示就是不顯示。查了查原因
StackOverFlow上的答案。
This is because if you use the support AppCompat ActionBar library and ActionBarActivity you should
create your menus in a different than the standard way of creating xml menus in ActioBarSherlock or the default ActionBar.
添加了第二行:<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/action_search" android:icon="@drawable/ic_action_search" android:title="@string/action_search" app:showAsAction="always" /> <item android:id="@+id/action_compose" android:icon="@drawable/ic_action_compose" android:title="@string/action_compose" app:showAsAction="always"/> </menu>
xmlns:app="http://schemas.android.com/apk/res-auto"
並且把下行的Android:showAsAction="always"改成
app:showAsAction="always"
查了官方文件也弄的不是很明白,總之是解決了。
Android在解決版本相容的問題上真的是很蛋疼。。。怎麼說呢,最近總結的經驗就是一定要Google,最好用英文查,百度一下午不如google幾分鐘解決問題 = =
順帶解決一下 不顯示app icon 的問題,查的醉醉的,在activity 的 oncreate 方法加入:
ActionBar actionBar = getSupportActionBar(); actionBar.setLogo(R.drawable.ic_launcher); actionBar.setDisplayUseLogoEnabled(true); actionBar.setDisplayShowHomeEnabled(true);