原文摘自:http://blog.csdn.net/android2me/article/details/8874846
1.Action Bar 介紹
我們能在應用中看見的actionbar一般就是下圖的樣子,比如快圖應用
1.App icon 應用的圖標,左側帶應用相當於back返回鍵
2.ViewControl
3.Action button 相當於普通的Button可以監聽點擊事件
4.Action overflow 三個點,相當於手機上的menu鍵,可以顯示隱藏的action button
下面是一個簡單的關於Action Bar的例子:
- package com.example.demo_actionbarbasic;
- import com.example.demo_actionbarbasic.R;
- import android.app.ActionBar;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- private MenuItem menuItem = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- // 通過hilde()和show()方法可以控制actionbar的隱藏和顯示
- // ActionBar actionBar = getActionBar();
- // actionBar.hide();
- // actionBar.show();
- }
- // 我們可以看到,actonbar的用法跟選項菜單是一樣的
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.activity_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case R.id.action_refresh:
- Toast.makeText(this, "Menu Item refresh selected",
- Toast.LENGTH_SHORT).show();
- break;
- case R.id.action_about:
- Toast.makeText(this, "Menu Item about selected", Toast.LENGTH_SHORT)
- .show();
- break;
- case R.id.action_edit:
- Toast.makeText(this, "Menu Item edit selected", Toast.LENGTH_SHORT)
- .show();
- break;
- case R.id.action_search:
- Toast.makeText(this, "Menu Item search selected",
- Toast.LENGTH_SHORT).show();
- break;
- case R.id.action_help:
- Toast.makeText(this, "Menu Item settings selected",
- Toast.LENGTH_SHORT).show();
- break;
- default:
- break;
- }
- return super.onOptionsItemSelected(item);
- }
- }
- <menu xmlns:android="http://schemas.android.com/apk/res/android" >
- <item
- android:id="@+id/menu_settings"
- android:orderInCategory="100"
- android:showAsAction="never"
- android:title="settings"/>
- <item
- android:id="@+id/action_refresh"
- android:icon="@drawable/navigation_refresh"
- android:orderInCategory="101"
- android:showAsAction="ifRoom|withText"
- android:title="refresh"/>
- <item
- android:id="@+id/action_about"
- android:icon="@drawable/action_about"
- android:orderInCategory="101"
- android:showAsAction="ifRoom"
- android:title="about"/>
- <item
- android:id="@+id/action_search"
- android:icon="@drawable/action_search"
- android:orderInCategory="103"
- android:showAsAction="ifRoom"/>
- <item
- android:id="@+id/action_edit"
- android:icon="@android:drawable/ic_menu_edit"
- android:orderInCategory="105"
- android:showAsAction="ifRoom"
- android:title="edit"/>
- <item
- android:id="@+id/action_help"
- android:showAsAction="always"
- android:title="help"/>
- <item
- android:id="@+id/action_email"
- android:icon="@android:drawable/ic_dialog_email"
- android:orderInCategory="106"
- android:showAsAction="ifRoom"
- android:title="email"/>
- </menu>
onCreateOptionsMenu()方法用來加載menu文件夾中定義的xml文件,用來顯示action bar。onOptionsItemSelected()方法用來加入點擊事件。
效果圖:
左圖的效果我們看到只能顯示兩個action button,由於屏幕的空間有限,其他的action button會被隱藏。橫屏的時候我們可以顯示4個,還有3個被隱藏起來了。當我們按手機上的更多鍵時可以顯示出來關於action button的文字信息,一定要在item標簽中加入title屬性。
android:showAsAction="ifRoom"ifRomm表示有空間的時候顯示。
android:showAsAction="always"表示總是顯示
android:showAsAction="ifRoom|withText"有空間的時候同時顯示title標題
其他屬性可以自己試試。
2.顯示3個點的更多action button
從上面的代碼我們知道,即使我們橫屏也顯示不出全部action button。我們可以加入3個點的action button來用下拉顯示的方式,顯示跟多的action button。在網上的信息得知,只要你的手機有menu鍵actionbar就不會顯示3個點的更多或者說3個點的menu按鈕。從上面的代碼我們知 道,即使我們橫屏也顯示不出全部action button。我們可以加入3個點的action button來用下拉顯示的方式,顯示跟多的action button。在網上的信息得知,只要你的手機有menu鍵actionbar就不會顯示3個點的更多或者說3個點的menu按鈕。
- private void getOverflowMenu() {
- try {
- ViewConfiguration config = ViewConfiguration.get(this);
- Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
- if(menuKeyField != null) {
- menuKeyField.setAccessible(true);
- menuKeyField.setBoolean(config, false);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
在onCreat()方法中調用這個方法可以顯示3個點的menu按鈕。下圖是按下3個點的action button的效果
代碼:Demo_ActionBar3dot
動態action button
用到了MenuItem 類的,setActionView()和collapseActionView()這兩個方法。 這個例子的效果是當我們點擊refresh action button的時候會顯示進度條。
- package com.example.demo_actionbar;
- import android.app.ActionBar;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.view.KeyEvent;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.EditText;
- import android.widget.ProgressBar;
- import android.widget.TextView;
- import android.widget.TextView.OnEditorActionListener;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- private MenuItem menuItem = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.activity_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // TODO Auto-generated method stub
- switch (item.getItemId()) {
- case R.id.action_refresh:
- menuItem = item;
- menuItem.setActionView(R.layout.progressbar);
- TestTask task = new TestTask();
- task.execute("test");
- break;
- case R.id.action_about:
- Toast.makeText(this, "Menu Item about selected", Toast.LENGTH_SHORT)
- .show();
- break;
- default:
- break;
- }
- return super.onOptionsItemSelected(item);
- }
- private class TestTask extends AsyncTask<String, Void, String> {
- @Override
- protected String doInBackground(String... params) {
- // Simulate something long running
- try {
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- return null;
- }
- @Override
- protected void onPostExecute(String result) {
- menuItem.collapseActionView(); // 這個方法需要 API 14 以上
- menuItem.setActionView(null);
- }
- };
- }
Actionbar之spinner實現drop-down Navigation
1.首先需要一個SpinnerAdapter設置下拉item的內容和顯示的layout
2.實現ActionBar.OnNavigationListener這個接口,接口中有點擊item的事件
3.設置navigation mode例如
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
4.用 setListNavigationCallbacks()方法來實現下拉選項,例如
actionBar.setListNavigationCallbacks(mSpinnerAdapter, mNavigationCallback);
效果圖:
代碼:
- package in.wptrafficanalyzer.actionbardropdownnavigation;
- import android.app.ActionBar;
- import android.app.ActionBar.OnNavigationListener;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.ArrayAdapter;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- /** An array of strings to populate dropdown list */
- String[] actions = new String[] { "Bookmark", "Subscribe", "Share" };
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- /** Create an array adapter to populate dropdownlist */
- ArrayAdapter<String> adapter = new ArrayAdapter<String>(
- getBaseContext(),
- android.R.layout.simple_spinner_dropdown_item, actions);
- /** Enabling dropdown list navigation for the action bar */
- getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
- /** Defining Navigation listener */
- ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
- @Override
- public boolean onNavigationItemSelected(int itemPosition,
- long itemId) {
- Toast.makeText(getBaseContext(),
- "You selected : " + actions[itemPosition],
- Toast.LENGTH_SHORT).show();
- return false;
- }
- };
- /**
- * Setting dropdown items and item navigation listener for the actionbar
- */
- getActionBar().setListNavigationCallbacks(adapter, navigationListener);
- }
- }
代碼下載:Demo_ActionBarDropdownNavigation
Action Bar之Contextual action bar
兩張圖,前一張是沒有顯示contextual action bar 的時候,后面那張是用戶長點擊EditText顯示contextual action bar的效果。contextual action bar 也能加入menu item 並對menu item 進行監聽。在contextual action bar 中顯示 menu item 需要在 /res/menu/ 文件夾中加入布局文件。
代碼:
- package com.example.demo_actionbarcontextual;
- import com.example.demo_actionbarcontextual.R;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.ActionMode;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- protected Object mActionMode;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- View view = findViewById(R.id.myView);
- // 對EditText設置長點擊事件,用來顯示 contextual action bar
- view.setOnLongClickListener(new View.OnLongClickListener() {
- public boolean onLongClick(View view) {
- if (mActionMode != null) {
- return false;
- }
- // Start the Contextual Action Bar using the ActionMode.Callback defined above
- mActionMode = MainActivity.this
- .startActionMode(mActionModeCallback);
- view.setSelected(true);
- return true;
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- MenuInflater inflater = getMenuInflater();
- inflater.inflate(R.menu.mainmenu, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- Toast.makeText(this, "Just a test", Toast.LENGTH_SHORT).show();
- return true;
- }
- private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
- // Called when the action mode is created; startActionMode() was called
- public boolean onCreateActionMode(ActionMode mode, Menu menu) {
- // Inflate a menu resource providing context menu items
- MenuInflater inflater = mode.getMenuInflater();
- // R.menu.contextual 是 contextual action bar 的布局文件, 在 /res/menu/ 文件夾下
- inflater.inflate(R.menu.contextual, menu);
- return true;
- }
- // Called each time the action mode is shown. Always called after
- // onCreateActionMode, but
- // may be called multiple times if the mode is invalidated.
- public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
- return false; // Return false if nothing is done
- }
- // 當用戶點擊 contextual action bar 的 menu item 的時候產生點擊事件
- public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
- switch (item.getItemId()) {
- case R.id.toast:
- Toast.makeText(MainActivity.this, "Selected menu",
- Toast.LENGTH_LONG).show();
- mode.finish(); // 關閉 contextual action bar
- return true;
- default:
- return false;
- }
- }
- // Called when the user exits the action mode
- public void onDestroyActionMode(ActionMode mode) {
- mActionMode = null;
- }
- };
- }
下載:Demo_ActionBarContextual
Action bar 之 navigation tabs
左圖是豎直屏幕的效果,右圖是橫屏的效果:
代碼:
- package de.arvidg.exampleactionbartabs;
- import android.app.ActionBar;
- import android.app.ActionBar.Tab;
- import android.app.Activity;
- import android.app.Fragment;
- import android.app.FragmentTransaction;
- import android.content.Context;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.widget.Toast;
- public class StartActivity extends Activity {
- public static Context appContext;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- appContext = getApplicationContext();
- // ActionBar
- ActionBar actionbar = getActionBar();
- // 設置action bar 的 navigation mode
- actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
- // 添加 action bar 的 tabs
- ActionBar.Tab PlayerTab = actionbar.newTab().setText("Fragment A");
- ActionBar.Tab StationsTab = actionbar.newTab().setText("Fragment B");
- // 實例化 fragment action bar 是用 fragment 來顯示的
- Fragment PlayerFragment = new AFragment();
- Fragment StationsFragment = new BFragment();
- // 對 tabs 設置監聽事件
- PlayerTab.setTabListener(new MyTabsListener(PlayerFragment));
- StationsTab.setTabListener(new MyTabsListener(StationsFragment));
- // 最后把 tabs 加入監聽事件
- actionbar.addTab(PlayerTab);
- actionbar.addTab(StationsTab);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- MenuInflater inflater = getMenuInflater();
- inflater.inflate(R.menu.main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case R.id.menuitem_search:
- Toast.makeText(appContext, "search", Toast.LENGTH_SHORT).show();
- return true;
- case R.id.menuitem_add:
- Toast.makeText(appContext, "add", Toast.LENGTH_SHORT).show();
- return true;
- case R.id.menuitem_share:
- Toast.makeText(appContext, "share", Toast.LENGTH_SHORT).show();
- return true;
- case R.id.menuitem_feedback:
- Toast.makeText(appContext, "feedback", Toast.LENGTH_SHORT).show();
- return true;
- case R.id.menuitem_about:
- Toast.makeText(appContext, "about", Toast.LENGTH_SHORT).show();
- return true;
- case R.id.menuitem_quit:
- Toast.makeText(appContext, "quit", Toast.LENGTH_SHORT).show();
- return true;
- }
- return false;
- }
- // @Override
- // protected void onSaveInstanceState(Bundle outState) {
- // super.onSaveInstanceState(outState);
- // outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
- // }
- }
- // 實例化 tabs 的監聽類
- class MyTabsListener implements ActionBar.TabListener {
- public Fragment fragment;
- // 傳入監聽的 tab 的 fragment
- public MyTabsListener(Fragment fragment) {
- this.fragment = fragment;
- }
- // 重復兩次以上點擊 tab
- @Override
- public void onTabReselected(Tab tab, FragmentTransaction ft) { // ft 用來控制 fragment
- Toast.makeText(StartActivity.appContext, "Reselected!",
- Toast.LENGTH_SHORT).show();
- }
- // 就點擊一次
- @Override
- public void onTabSelected(Tab tab, FragmentTransaction ft) {
- ft.replace(R.id.fragment_container, fragment);
- }
- // 不點擊
- @Override
- public void onTabUnselected(Tab tab, FragmentTransaction ft) {
- ft.remove(fragment);
- }
Action bar 之 ShareActionProvider
1.首先要在menu布局文件的item標簽中加入
android:actionProviderClass="android.widget.ShareActionProvider"
2.得到ShareActionProvider的實例
provider = (ShareActionProvider) menu.findItem(R.id.menu_share)
.getActionProvider();
3.設置Intent
效果圖:
代碼:
- package com.example.demo_shareactionprovider;
- import android.os.Bundle;
- import android.app.Activity;
- import android.content.Intent;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.ShareActionProvider;
- public class MainActivity extends Activity {
- ShareActionProvider provider = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.activity_main, menu);
- // Get the ActionProvider
- provider = (ShareActionProvider) menu.findItem(R.id.menu_share)
- .getActionProvider();
- // Initialize the share intent
- Intent intent = new Intent(Intent.ACTION_SEND);
- intent.setType("text/plain");
- intent.putExtra(Intent.EXTRA_TEXT, "Text I want to share");
- provider.setShareIntent(intent);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case R.id.menu_share:
- break;
- default:
- break;
- }
- return super.onOptionsItemSelected(item);
- }
- }
/res/menu/activity_main.xml
- <menu xmlns:android="http://schemas.android.com/apk/res/android" >
- <item
- android:id="@+id/menu_share"
- android:actionProviderClass="android.widget.ShareActionProvider"
- android:showAsAction="ifRoom"
- android:title="Share"/>
- <item
- android:id="@+id/item1"
- android:icon="@android:drawable/ic_menu_call"
- android:showAsAction="ifRoom"
- android:title="">
- </item>
- </menu>
The ShareActionProvider now handles all user interaction with the item and you do not need to handle click events from the onOptionsItemSelected() callback method.
上面是官方文檔上給的: 就是說無需在onOptionsItemSelected()這個回調方法中再去處理了。
代碼:Demo_ShareActionProvider
Action Bar 之 style
你可以用android的style和theme來自定義action bar的風格和主題
android:windowActionBarOverlay 這個屬性是用來定義actionbar和其下方視圖的位置關系的。默認false,當設置成true時,表示activity layout 就是說你的下方的視圖將覆蓋整個屏幕。這樣設置的好處就是說,當你隱藏action bar的時候,視圖不會改變位置。當我們把action bar設置成半透明的時候,我們也能看見其下面的內容,這樣的界面對用戶來說更加有好。
<SomeView android:layout_marginTop="?android:attr/actionBarSize" />
上面這個屬性可以設置activity layout距離屏幕頂端的距離。這樣設置可以防止被action bar覆蓋下方內容。
下面是一個簡單的關於如何改變antion bar 字體、分割圖片、背景的一個style
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <!-- the theme applied to the application or activity -->
- <style name="CustomActivityTheme" parent="@android:style/Theme.Holo">
- <item name="android:actionBarTabTextStyle">@style/CustomTabTextStyle</item>
- <item name="android:actionBarDivider">@drawable/ab_divider</item>
- <item name="android:actionBarItemBackground">@drawable/ab_item_background</item>
- </style>
- <!-- style for the action bar tab text -->
- <style name="CustomTabTextStyle" parent="@android:style/TextAppearance.Holo">
- <item name="android:textColor">#2456c2</item>
- </style>
- </resources>
能設置的關於action bar的風格(style)的屬性有:
android:actionButtonStyle // Defines a style resource for the action item buttons.
android:actionBarItemBackground //Defines a drawable resource for each action item's background. (Addedin API level 14.)
android:itemBackground // Defines a drawable resource for each overflow menu item's background.
android:actionBarDivider // Defines a drawable resource for the divider between action items.(Added in API level 14.)
android:actionMenuTextColor //Defines a color for text that appears in an action item.
android:actionMenuTextAppearance //Defines a style resource for text that appears in an action item.
android:actionBarWidgetTheme //Defines a theme resource for widgets that are inflated into the actionbar as action views. (Added in API level 14.)
下面的網址是源碼中的styles.xml和themes.xml文件,包含了系統中所有的樣式和主題。根據自己的需要可以改變action bar的顯示風格。
https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/themes.xml
例子1:怎么改變tab的字體大小、字體顏色等
- <pre name="code" class="html">在themes.xml文件中關於tab的屬性有:</pre><br>
- <br>
- <pre></pre>
- <pre name="code" class="html">android:actionBarTabStyle // Defines a style resource for tabs in the action bar.</pre>android:actionBarTabBarStyle // Defines a style resource for the thin bar that appears below the navigation tabs.<br>
- android:actionBarTabTextStyle //Defines a style resource for text in the navigation tabs.
- <p><br>
- </p>
- <p>在themes.xml文件中找到:</p>
- <p></p>
- <p></p>
- <pre name="code" class="html"><itemnameitemname="actionBarTabTextStyle">@style/Widget.Holo.Light.ActionBar.TabText</item></pre>
- <p></p>
- <p>在style.xml文件中找到</p>
- <pre name="code" class="html"><style name="Widget.Holo.Light.ActionBar.TabText" parent="Widget.Holo.ActionBar.TabText">
- </pre>
- <p><br>
- </p>
- 在style.xml文件中通過Widget.Holo.ActionBar.TabText,我們可以找到下面<br>
- <pre name="code" class="html"><style name="Widget.Holo.ActionBar.TabText" parent="Widget.ActionBar.TabText">
- <item name="android:textAppearance">@style/TextAppearance.Holo.Medium</item>
- <item name="android:textColor">?android:attr/textColorPrimary</item>
- <item name="android:textSize">12sp</item>
- <item name="android:textStyle">bold</item>
- <item name="android:textAllCaps">true</item>
- <item name="android:ellipsize">marquee</item>
- <item name="android:maxLines">2</item>
- </style>
- </pre>
- <p><br>
- </p>
- <p>下面是我們工程中styles.xml的內容:<br>
- </p>
- <pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
- <resources xmlns:android="http://schemas.android.com/apk/res/android">
- <style name="CustomActivityThemo" parent="@android:style/Theme.Holo.Light">
- <item name="android:actionBarTabTextStyle">@style/CustomTabTextStyle</item>
- </style>
- <style name="CustomTabTextStyle" parent="@android:style/Widget.ActionBar.TabText">
- <item name="android:textSize">25sp</item>
- <item name="android:textColor">#FF0000</item>
- <item name="android:textStyle">italic|bold</item>
- </style>
- </resources>
- </pre><br>
- 我們必須在manifest.xml文件中的activity標簽中設置<br>
- <br>
- <p></p>
- <pre name="code" class="html"><activity
- <span style="white-space:pre"> </span>android:theme="@style/CustomActivityThemo" ></pre><pre name="code" class="html"><span style="white-space:pre"> </span>....
- </activity>
- </pre>
- <p></p>
- <p><br>
- </p>
- 代碼:Demo_ActionBarTabsColor<br>
- <p><br>
- </p>
- <p>例子2:改變action bar中tab的indicator(下面的那條橫線)的顏色<br>
- styles.xml文件內容:</p>
- <p><br>
- </p>
- <pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
- <resources xmlns:android="http://schemas.android.com/apk/res/android">
- <!-- the theme applied to the application or activity -->
- <style name="CustomActivityTheme" parent="@android:style/Theme.Holo.Light">
- <item name="android:actionBarTabTextStyle">@style/CustomTabTextStyle</item>
- <item name="android:actionBarTabBarStyle">@android:color/holo_orange_dark</item>
- <item name="android:actionBarTabStyle">@style/ActionBarTabStyle.Example</item>
- </style>
- <!-- style for the action bar tab text -->
- <style name="CustomTabTextStyle" parent="@android:style/TextAppearance.Holo">
- <item name="android:textColor">#2456c2</item>
- </style>
- <style name="ActionBarTabStyle.Example" parent="@android:style/Widget.Holo.Light.ActionBar.TabView">
- <item name="android:background">@drawable/tab_indicator_ab_example</item>
- </style>
- </resources>
- </pre><br>
- <br>
- <p></p>
- <p>android:background這個屬性定義了tab的顯示風格,對應的是一個xml文件不是圖片<br>
- </p>
- <p></p>
- <p><br>
- </p>
- <p><br>
- </p>
- <p>tab_indicator_ab_example.xml文件的內容:</p>
- <p></p>
- <pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <!-- Non focused states -->
- <item android:drawable="@android:color/transparent" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>
- <item android:drawable="@drawable/tab_selected_example" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>
- <!-- Focused states -->
- <item android:drawable="@drawable/tab_unselected_focused_example" android:state_focused="true" android:state_pressed="false" android:state_selected="false"/>
- <item android:drawable="@drawable/tab_selected_focused_example" android:state_focused="true" android:state_pressed="false" android:state_selected="true"/>
- <!-- Pressed -->
- <!-- Non focused states -->
- <item android:drawable="@drawable/tab_unselected_pressed_example" android:state_focused="false" android:state_pressed="true" android:state_selected="false"/>
- <item android:drawable="@drawable/tab_selected_pressed_example" android:state_focused="false" android:state_pressed="true" android:state_selected="true"/>
- <!-- Focused states -->
- <item android:drawable="@drawable/tab_unselected_pressed_example" android:state_focused="true" android:state_pressed="true" android:state_selected="false"/>
- <item android:drawable="@drawable/tab_selected_pressed_example" android:state_focused="true" android:state_pressed="true" android:state_selected="true"/>
- </selector>
- </pre><pre name="code" class="html"></pre>代碼:Demo_ActionBarTabsColor<p></p><p>參考文獻:</p><p>http://www.vogella.com/articles/AndroidActionBar/article.htmlhttp://developer.android.com/guide/topics/ui/actionbar.html</p><p></p>