導航:首頁 > 操作系統 > android修改actionbar

android修改actionbar

發布時間:2022-11-27 21:14:17

1. android 怎麼在fragment裡面改變actionbar的背景色

要修改actionBar ,你要在這個裝載fragment的Activity中修改
直接在Activity中,調用getActionBar(); 得到actionBar 再做修改
當然你也可以在fragment調用getActivity().getActionBar() 來得到這個actionBar

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的背景顏色

Android動態更改actionbar的背景顏步驟:

在backgroud下面再寫一個style,修改values-v14文件夾下的style.xml文件
[html] view plain
<resources xmlns:android="http://schemas.android.com/apk/res/android">

<!--
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
<!-- API 14 theme customizations can go here. -->
<item name="android:actionBarStyle">@style/my_actionbar_style</item>
</style>

<style name="my_actionbar_style" parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#647b97</item>
<item name="android:titleTextStyle">@style/AcBar_titleStyle</item>
[html] view plain
<item name="android:backgroundStacked">#4A708B</item> <!-- 分離成tab時的tab顏色 -->
[html] view plain
<item name="android:backgroundSplit">@color/ab_backgroundSplit_color</item>
</style>
<style name="AcBar_titleStyle">

<item name="android:textColor">#FFFFFF</item>
</style>

</resources>

<item name="android:backgroundSplit">@color/ab_backgroundSplit_color</item> actionbar item
這樣就分離在底部時候的顏色。

4. 如何讓android的actionbar浮動且透明

如上圖所示,谷歌地圖的actionbar是透明的,且浮動在整個布局之上,沒有佔用布局空間。其實要做到這樣的效果,我們首先想到的是兩個方面:
1.將讓actionbar浮動起來。
2.給actionbar一個背景,可以為顏色也可以為圖片。
下面我以背景色為顏色舉個例子。
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);ActionBar actionBar = getActionBar();actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#330000ff")));

google的actionbar是分為上下兩欄顯示的,上面的代碼只能設置頂部actionbar的背景色,為了讓下面的背景色一致,還需要添加一行代碼:
actionBar.setSplitBackgroundDrawable(newColorDrawable(Color.parseColor("#330000ff")));

如果我們不想在代碼中設置actionbar背景,而是在style中修改xml,可以自定義actionbar,然後設置下面三個屬性:
<style name="MyActionBar" parent="@android:style/Widget.Holo.ActionBar"> <item name="android:background">@drawable/ab_stacked_solid_inverse_holo</item> <item name="android:backgroundStacked">#346c36</item> <item name="android:backgroundSplit">@drawable/ab_stacked_solid_inverse_holo</item> </style>

其中的background和setBackgroundDrawable對應,backgroundSplit和setSplitBackgroundDrawable對應,而backgroundStacked代表的是actionbar選項卡的背景色。
但是actionbar的背景如果設置為純色,或者為不透明的圖片,那麼上面的方法無法使actionbar達到透明的效果。注意上面的代碼中,我們的顏色值並不是純色的而是有一定的透明度。#330000ff的前兩位代表透明度。

5. 如何調整Android ActionBar向上按鈕與屏幕左側的距離

可以用 Toolbar 替代 ActionBar:
// Set a Toolbar to act as the ActionBar for this Activity window.
setSupportActionBar(android.support.v7.widget.Toolbar toolbar)
如果是在 xml 中定義的 Toolbar,修改屬性值為 0 就可以移除 Toolbar 左側和右側的空白。
<android.support.v7.widget.Toolbar
xmlns:app="schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primaryColor"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:contentInsetRight="0dp"
android:contentInsetEnd="0dp"
app:contentInsetRight="0dp"
app:contentInsetEnd="0dp" />

6. android 怎麼修改actionbar 的標題

設置title就會提示文字了。 ActionBar 標題欄常用屬性: 1. showAsAction屬性用來定義每個Action是如何顯示的 2. always表示永遠顯示在ActionBar中,如果屏幕空間不夠則無法顯示 3. ifRoom表示屏幕空間夠的情況下顯示在ActionBar中,不夠的話就顯示在overflow中 4. never則表示永遠顯示在overflow中

7. 如何更改Android中ActionBar的顏色

1、打開values下的styles.xml
發現有以下代碼:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/myBarColor</item>
<item name="colorPrimaryDark">@color/myBarColor</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
那麼其中的colorPrimary、colorPrimaryDark、colorAccent分別代表什麼呢?看下圖就可以一目瞭然:
style
2、更改colorPrimary和colorPrimaryDark
將colorPrimary、colorPrimaryDark的顏色換成你需要改變的顏色即可。
比如,我在values/colors.xml中定義了顏色:
<color name="myBarColor">#0682AF</color>
那麼將colorPrimary、colorPrimaryDark的顏色換成@color/myBarColor即可:
<item name="colorPrimary">@color/myBarColor</item>
<item name="colorPrimaryDark">@color/myBarColor</item>

8. Android 5.0 ActionBar的Title顏色怎麼修改

在backgroud下面再寫一個style。修改values-v14文件夾下的style.xml文件

<resources xmlns:android="http://schemas。android。com/apk/res/android">

<!--
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
<!-- API 14 theme customizations can go here. -->
<item name="android:actionBarStyle">@style/my_actionbar_style</item>
</style>

<style name="my_actionbar_style" parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#647b97</item>
<item name="android:titleTextStyle">@style/AcBar_titleStyle</item>
<item name="android:backgroundStacked">#4A708B</item> <!-- 分離成tab時的tab顏色 -->
<item name="android:backgroundSplit">@color/ab_backgroundSplit_color</item>
</style>
<style name="AcBar_titleStyle">

<item name="android:textColor">#FFFFFF</item>
</style>

</resources>

9. Android動態改變actionbar的圖標

就是這里,怎樣才能在代碼中動態改變這里的圖標。
比如我得到了某種狀態,我想給圖標換個顏色。
onPrepareOptionsMenu這個函數,我試了試,即使按物理按鍵,對ActionBar上的menu似乎不起作用,那如何才能改變呢?

或者,如何才能在代碼中獲取在

public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

這里載入過的menu呢?

補充說明:

public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);

MenuItem menuItem = menu.findItem(R.id.create_connection);
if (isCreateConnectionSuccess) {
menuItem.setIcon(R.drawable.apps_bright);
menuItem.setTitle("改變過");
}
return true;
}

這段代碼我試過,但是ActionBar是個神奇的東西,它只改變了Title,而Icon卻沒改變。
我猜原因可能是因為ActionBar上的Menu一直顯示,所以對它沒有「顯示前」?

閱讀全文

與android修改actionbar相關的資料

熱點內容
我的世界中如何查看伺服器的人數 瀏覽:618
台式機改為網路伺服器有什麼好處 瀏覽:960
騰訊雲輕量應用伺服器如何登陸 瀏覽:620
考研復試c語言編譯器 瀏覽:150
安卓的字體怎麼變粗 瀏覽:253
java錯誤無法載入主類 瀏覽:348
程序員考試考什麼文憑 瀏覽:883
pdf版破解 瀏覽:522
安卓系統如何重啟 瀏覽:174
小天才app鬧鍾怎麼改 瀏覽:962
司馬彥PDF 瀏覽:885
動力轉向編程 瀏覽:831
史瓦格期貨基本分析pdf 瀏覽:811
怎麼更改appid名字 瀏覽:235
抖音很解壓的東西 瀏覽:520
怎麼在app上進行機場升艙 瀏覽:133
fx3ga用什麼編程軟體 瀏覽:498
深度學習演算法樣本數量 瀏覽:966
電腦文件夾打開畫面塊狀 瀏覽:790
固態硬碟與機械盤的加密方法 瀏覽:634