1. android的actionbar的溢出菜單不顯示icon,怎樣弄成微信那樣顯示icon
直接給你一個方法:在onMenuOpened(int featureId, Menu menu)方法中調用
/**
* 利用反射讓隱藏在Overflow中的MenuItem顯示Icon圖標
* @param featureId
* @param menu
* onMenuOpened方法中調用
*/
public static void setOverflowIconVisible(int featureId, Menu menu) {
if (featureId == Window.FEATURE_ACTION_BAR && menu != null) {
if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
try {
Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
} catch (Exception e) {
}
}
}
}
2. android 的action bar有什麼作用
下面是一個actionbar的使用截圖,來源於android學習手冊,360手機助手中可下載,裡面有108個例子,源碼還有文檔
Action bar是一個標識應用程序和用戶位置的窗口功能,並且給用戶提供操作和導航模式。在大多數的情況下,當你需要突出展現用戶行為或全局導航的activity中使用action bar,因為action bar能夠使應用程序給用戶提供一致的界面,並且系統能夠很好根據不同的屏幕配置來適應操作欄的外觀。你能夠用ActionBar的對象的API來控制操作欄的行為和可見性,這些API被添加在Android3.0(API 級別 11)中。
Action bar的主要目的是:
1.提供一個用於識別應用程序的標示和用戶的位置的專用空間。
這個空間的左邊是應用的圖標或logo,以及Activity的標題。但是,如果是像當前選擇的標簽這樣的標識當前View對象的導航標簽,你可以選擇刪除Activity的標題。
2.在不同的應用程序之間提供一致的導航和視覺體驗。
Action bar提供了用於Fragment間切換的內置導航標簽。它還提供了一個用於替換導航模式或優化當前視覺效果(如按照不同條件排序的列表)的下拉列表。
3.突出Activity的關鍵操作(如「搜索」、「創建」、「共享」等),並且在可預見的方法內給用戶提供快捷的訪問。
對於關鍵的用戶操作,你能夠通過把選項菜單項作為操作項直接放到操作欄中,從而提供快捷的訪問。操作項目還能提供一個操作窗口,這個窗口給更直接的操作行為提供一個嵌入的窗口部件。沒有改進成操作項的菜單項在溢出菜單中還是有效的,用戶既可以使用設備上的菜單按鈕(設備上有按鈕的時候),也可以使用操作欄中的溢出菜單按鈕(當設備上不包含菜單按鈕時)來顯示這些操作項目。
上面的總結一下:Action bar就是替換3.0以前的tittle bar和menu。
圖1. Honeycomb Gallery應用中的操作欄,從左邊開始,依次是logo、導航選項標簽和操作項(在右邊插入的一個懸浮菜單按鈕)。
Note:If you're looking for information about the contextual action bar for displaying contextual action items, see theMenuguide.
Action Bar Design For design guidelines, read Android Design'sAction Barguide.
添加Action Bar
從Android3.0(API級別 11)開始,Action bar被包含在所有的使用Theme.Hole主題的Activity(或者是這些Activity的子類)中,當targetSdkVersion或minSdkVersion屬性被設置為「11」或更大的數值是,這個主題是默認的主題一。如:
[html] view plain print?
<manifest...>
<uses-sdkandroid:minSdkVersion="4"
android:targetSdkVersion="11"/>
...
</manifest>
<manifest ... >
<uses-sdk android:minSdkVersion="4"
android:targetSdkVersion="11" />
...
</manifest>
在這個例子中,應用程序要求最小的API版本級別是4(Android 1.6),但是它還要求了目標API版本級別是11(Android 3.0)。這樣,當應用程序運行在Android3.0或更高的版本上時,系統就會給每個Activity應用holographic 主題,這樣,每個Activity就會包含Action bar。
如果你想使用ActionBar API來進行添加導航模式和修改操作欄樣式的操作,你應該把minSdkVersion屬性設置為「11」或更大的值。有一些方法可以使你的應用支持更舊的Android版本,同時在API等級為11或更高的API等級的機器的使你的應用支持一些Action bar apis。為了保持後向兼容,請參考邊框內的內容(邊框內容如下)。
Remaining backward-compatible
If you want to provide an action bar in your applicationandremain compatible with versions of Android older than 3.0, you need to create the action bar in your activity's layout (because theActionBarclass is not available on older versions).
To help you, theAction Bar Compatibilitysample app provides an API layer and action bar layout that allows your app to use some of theActionBarAPIs and also support older versions of Android by replacing the traditional title bar with a custom action bar layout.
刪除Action bar
如果你不想要Action bar,把Activity的主題設置為Theme.Holo.NoActionBar就可以了,如:
[html] view plain print?
<activityandroid:theme="@android:style/Theme.Holo.NoActionBar">
<activity android:theme="@android:style/Theme.Holo.NoActionBar">
或者使用Action bar的 hide()方法,如下:
[java] view plain print?
ActionBaractionBar=getActionBar();
actionBar.hide();
ActionBar actionBar = getActionBar();
actionBar.hide();
當Action bar隱藏時,系統會調整你的Activity來填充當前有效的屏幕空間。你能夠使用show()方法來再次顯示操作欄。
在隱藏和刪除Action bar時,要當心為了適應被Action bar佔用的空間而導致的Activity的重新布局。如果你的Activity有規律的隱藏和顯示Action bar,你可能想要使用覆蓋模式。覆蓋模式在Activity的頂部描畫操作欄,而不是在它們所擁有的屏幕的區域。這樣,在Action bar隱藏和重新顯示時,你的布局保持不變。要使用覆蓋模式,就要給Activity創建一個主題,並且把android:windowActionBarOverlay屬性設置為true。
提示:如果你有一個刪除了Action bar的定製化的Activity主題,它把android:windowActionBar樣式屬性設置為false。但是,如果你使用了刪除Action bar的一個主題,那麼,創建窗口將不允許Action bar再顯示,因此,你不能在以後給這個Activity添加Action bar---因為getActionBar()方法將返回null。
添加操作項
有些時候,你可能想要讓用戶直接訪問選項菜單中的一個項目,因此你要把應該在Action bar中顯示的菜單項作為一個操作項來聲明。操作項能夠能夠包含一個圖標或文本標題。如果一個菜單項不作為一個操作項顯示,那麼系統就會把它放到懸浮菜單中。懸浮菜單既可以通過設備的Menu按鈕來顯示,也可以在Action bar中一個額外的按鈕來顯示。
當Activity首次啟動時,系統會調用onCreateOptionsMenu()方法給你的Activity組裝Action bar和懸浮菜單。在這個回調方法中應該載入在XML文件中定義的菜單項資源,如:
[java] view plain print?
@Override
(Menumenu){
MenuInflaterinflater=getMenuInflater();
inflater.inflate(R.menu.main_activity,menu);
returntrue;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity, menu);
return true;
}
圖2. 帶有圖標和文本標題的兩個操作項,和懸浮菜單按鈕。
在XML文件中,你能夠通過給<item>元素聲明android:showAsAction=」ifRoom」屬性,請求把一個菜單項作為一個操作項來顯示。用這種方式,只在有有效的空間時,菜單項才能顯示在Action bar中。如果沒有足夠的空間,這個菜單項會顯示在懸浮菜單中。
如果你的菜單項支持標題和圖標---帶有android:title和android:icon屬性---那麼默認情況下,操作項僅顯示圖標。如果你要顯示文本標題,就要給android:showAsAction屬性添加withText設置,如:
3. 如何評價android中的actionbar
在Android3.0之後,Google對UI導航設計上進行了一系列的改革,其中有一個非常好用的新功能就是引入的ActionBar,他用於取代3.0之前的標題欄,並提供更為豐富的導航效果。
一、添加ActionBar
1、不需要導入ActionBar,因為在android3.0或者以上版本中,ActionBar已經默認的包含在Activity中
2、在Android3.0的默認主題「holographic」中,已經創造了ActionBar
3、當android:minSdkVersion 或者 android:targetSdkVersion 屬性被設置成11或者更高時,應用會被認為是Android3.0版本(默認含有ActionBar)
二、隱藏ActionBar
1、我們可以通過把主題設置為Theme.Holo.NoActionBar為一個Activity去除掉ActionBar。<activity android:theme="@android:style/Theme.Holo.NoActionBar"> 2、我們也可以通過在代碼中調用show()或者hide()方法來相應的顯示或者隱藏ActionBar
3、在我們使用actionBar.hide();方法是系統默認會將ActionBar佔用的空間分配給界面,此時顯示的界面將會重新繪制。
同樣調用actionBar.show();時也會重新繪制界面。如果我們一個程序需要頻繁的顯示或者隱藏ActionBar的話,這樣
就不會有良好的效果。Google提供給一個屬性可以讓ActionBar浮在界面上,當然你可以讓ActionBar的背景為透明,這樣會有良好的體驗效果。
4. Android:在ActionBar中,如何為item自定義Selector
很多的ActionBar這種效果是通過image來實現的,所以,唯一的辦法就是創建一個自定義的style或者theme,可參考: http://android-developers.blogspot.com/2011/04/customizing-action-bar.html ,這里有個例子: http://code.google.com/p/styled-action-bar/
5. 怎樣在android 2.3中使用ActionBar
在你的menu的xml文件裡面, 自定義個一個當前包名的命名空間, 比如: test
然後在item標簽下添加屬性: test:showAsAction="always"
6. 如何使用android-support-V7包中ActionBar
以前3.0以前的版本要使用ActionBar,必須使用國外大牛寫的ActionBarSherlock這個開源項目。今年的Google/IO大會之後,Gooogle官方在android-support-v 7包中添加了ActionBar,開始讓2.1以後的版本支持ActionBar,從此以後曾經最火的Android開源項目ActionBarSherlock可以退出歷史舞台了。
要是用V7包中ActionBar也很簡單,但有一個需要主要的地方。有些人可能該開始僅僅是把android-support-v7-appcompat.jar導入項目中
具體使用 步驟 (針對於Eclipse):
Create a library project based on the support library code:
Make sure you have downloaded the Android Support Library using the SDK Manager .
Create a library project and ensure the required JAR files are included in the project's build path:
Select File > Import .
Select Existing Android Code Into Workspace and click Next .
Browse to the SDK installation directory and then to the Support Library folder. For example, if you are adding the appcompat project, browse to <sdk>/extras/android/support/v7/appcompat/ .
Click Finish to import the project. For the v7 appcompat project, you should now see a new project titled android-support-v7-appcompat .
In the new library project, expand the libs/ folder, right-click each .jar file and select Build Path > Add to Build Path . For example, when creating the the v7 appcompat project, add both the android-support-v4.jar and android-support-v7-appcompat.jar files to the build path.
Right-click the project and select Build Path > Configure Build Path .
In the Order and Export tab, check the .jar files you just added to the build path, so they are available to projects that depend on this library project. For example, the appcompat project requires you to export both the android-support-v4.jar and android-support-v7-appcompat.jar files.
Uncheck Android Dependencies .
Click OK to complete the changes.
7. android如何去掉actionbar
當使用Android中的ActionBar控制項時,如果想要隱藏上面的ActionBar,可以使用如下的代碼:
getSupportActionBar().hide();//隱藏掉整個ActionBar,包括下面的Tabs
上面的代碼會將整個ActionBar都隱藏掉,包括ActionBar中的Tab分頁標簽,如果想要保留分頁標簽的話,可以使用如下的代碼:
ActionBar actionBar = getSupportActionBar();//高版本可以換成 ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
//會保留tab標簽
另外還有一種更簡單的方式來移除ActionBar,在setContent之前調用下面這句,保證沒有ActionBar
requestWindowFeature(Window.FEATURE_NO_TITLE);