zoukankan      html  css  js  c++  java
  • Android ActionBar 一步一步分析 (转)

    原文摘自: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的例子:

    [java] view plaincopy
     
    1. package com.example.demo_actionbarbasic;  
    2. import com.example.demo_actionbarbasic.R;  
    3. import android.app.ActionBar;  
    4. import android.app.Activity;  
    5. import android.content.Intent;  
    6. import android.os.Bundle;  
    7. import android.view.Menu;  
    8. import android.view.MenuItem;  
    9. import android.widget.Toast;  
    10.   
    11. public class MainActivity extends Activity {  
    12.     private MenuItem menuItem = null;  
    13.       
    14.     @Override  
    15.     protected void onCreate(Bundle savedInstanceState) {  
    16.         super.onCreate(savedInstanceState);  
    17.         setContentView(R.layout.activity_main);  
    18.         // 通过hilde()和show()方法可以控制actionbar的隐藏和显示  
    19.         // ActionBar actionBar = getActionBar();  
    20.         // actionBar.hide();  
    21.         // actionBar.show();  
    22.     }  
    23.   
    24.     // 我们可以看到,actonbar的用法跟选项菜单是一样的  
    25.     @Override  
    26.     public boolean onCreateOptionsMenu(Menu menu) {  
    27.         // Inflate the menu; this adds items to the action bar if it is present.  
    28.         getMenuInflater().inflate(R.menu.activity_main, menu);  
    29.         return true;  
    30.     }  
    31.   
    32.     @Override  
    33.     public boolean onOptionsItemSelected(MenuItem item) {  
    34.         switch (item.getItemId()) {  
    35.         case R.id.action_refresh:  
    36.             Toast.makeText(this"Menu Item refresh selected",  
    37.                     Toast.LENGTH_SHORT).show();  
    38.             break;  
    39.         case R.id.action_about:  
    40.             Toast.makeText(this"Menu Item about selected", Toast.LENGTH_SHORT)  
    41.                     .show();  
    42.             break;  
    43.         case R.id.action_edit:  
    44.             Toast.makeText(this"Menu Item edit selected", Toast.LENGTH_SHORT)  
    45.                     .show();  
    46.             break;  
    47.         case R.id.action_search:  
    48.             Toast.makeText(this"Menu Item search selected",  
    49.                     Toast.LENGTH_SHORT).show();  
    50.             break;  
    51.         case R.id.action_help:  
    52.             Toast.makeText(this"Menu Item  settings selected",  
    53.                     Toast.LENGTH_SHORT).show();  
    54.             break;  
    55.         default:  
    56.             break;  
    57.         }  
    58.         return super.onOptionsItemSelected(item);  
    59.     }  
    60. }  
    [html] view plaincopy
     
    1. <menu xmlns:android="http://schemas.android.com/apk/res/android" >  
    2.     <item  
    3.         android:id="@+id/menu_settings"  
    4.         android:orderInCategory="100"  
    5.         android:showAsAction="never"  
    6.         android:title="settings"/>  
    7.     <item  
    8.         android:id="@+id/action_refresh"  
    9.         android:icon="@drawable/navigation_refresh"  
    10.         android:orderInCategory="101"  
    11.         android:showAsAction="ifRoom|withText"  
    12.         android:title="refresh"/>  
    13.     <item  
    14.         android:id="@+id/action_about"  
    15.         android:icon="@drawable/action_about"  
    16.         android:orderInCategory="101"  
    17.         android:showAsAction="ifRoom"  
    18.         android:title="about"/>  
    19.     <item  
    20.         android:id="@+id/action_search"  
    21.         android:icon="@drawable/action_search"  
    22.         android:orderInCategory="103"  
    23.         android:showAsAction="ifRoom"/>  
    24.     <item  
    25.         android:id="@+id/action_edit"  
    26.         android:icon="@android:drawable/ic_menu_edit"  
    27.         android:orderInCategory="105"  
    28.         android:showAsAction="ifRoom"  
    29.         android:title="edit"/>  
    30.     <item  
    31.         android:id="@+id/action_help"  
    32.         android:showAsAction="always"  
    33.         android:title="help"/>  
    34.     <item  
    35.         android:id="@+id/action_email"  
    36.         android:icon="@android:drawable/ic_dialog_email"  
    37.         android:orderInCategory="106"  
    38.         android:showAsAction="ifRoom"  
    39.         android:title="email"/>  
    40. </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按钮。

    [java] view plaincopy
     
    1. private void getOverflowMenu() {  
    2.    
    3.      try {  
    4.         ViewConfiguration config = ViewConfiguration.get(this);  
    5.         Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");  
    6.         if(menuKeyField != null) {  
    7.             menuKeyField.setAccessible(true);  
    8.             menuKeyField.setBoolean(config, false);  
    9.         }  
    10.     } catch (Exception e) {  
    11.         e.printStackTrace();  
    12.     }  
    13. }  


    在onCreat()方法中调用这个方法可以显示3个点的menu按钮。下图是按下3个点的action button的效果


    代码:Demo_ActionBar3dot



    动态action button 


    用到了MenuItem 类的,setActionView()和collapseActionView()这两个方法。 这个例子的效果是当我们点击refresh action button的时候会显示进度条。


    [java] view plaincopy
     
    1. package com.example.demo_actionbar;  
    2.   
    3. import android.app.ActionBar;  
    4. import android.app.Activity;  
    5. import android.content.Intent;  
    6. import android.os.AsyncTask;  
    7. import android.os.Bundle;  
    8. import android.view.KeyEvent;  
    9. import android.view.Menu;  
    10. import android.view.MenuItem;  
    11. import android.widget.EditText;  
    12. import android.widget.ProgressBar;  
    13. import android.widget.TextView;  
    14. import android.widget.TextView.OnEditorActionListener;  
    15. import android.widget.Toast;  
    16.   
    17. public class MainActivity extends Activity {  
    18.   
    19.     private MenuItem menuItem = null;  
    20.   
    21.     @Override  
    22.     protected void onCreate(Bundle savedInstanceState) {  
    23.         super.onCreate(savedInstanceState);  
    24.         setContentView(R.layout.activity_main);  
    25.     }  
    26.   
    27.     @Override  
    28.     public boolean onCreateOptionsMenu(Menu menu) {  
    29.         // Inflate the menu; this adds items to the action bar if it is present.  
    30.         getMenuInflater().inflate(R.menu.activity_main, menu);  
    31.         return true;  
    32.     }  
    33.   
    34.     @Override  
    35.     public boolean onOptionsItemSelected(MenuItem item) {  
    36.         // TODO Auto-generated method stub  
    37.         switch (item.getItemId()) {  
    38.         case R.id.action_refresh:  
    39.             menuItem = item;  
    40.             menuItem.setActionView(R.layout.progressbar);  
    41.             TestTask task = new TestTask();  
    42.             task.execute("test");  
    43.             break;  
    44.         case R.id.action_about:  
    45.             Toast.makeText(this"Menu Item about selected", Toast.LENGTH_SHORT)  
    46.                     .show();  
    47.             break;  
    48.         default:  
    49.             break;  
    50.         }  
    51.         return super.onOptionsItemSelected(item);  
    52.     }  
    53.   
    54.     private class TestTask extends AsyncTask<String, Void, String> {  
    55.   
    56.         @Override  
    57.         protected String doInBackground(String... params) {  
    58.             // Simulate something long running  
    59.             try {  
    60.                 Thread.sleep(2000);  
    61.             } catch (InterruptedException e) {  
    62.                 e.printStackTrace();  
    63.             }  
    64.             return null;  
    65.         }  
    66.   
    67.         @Override  
    68.         protected void onPostExecute(String result) {  
    69.             menuItem.collapseActionView(); // 这个方法需要 API 14 以上  
    70.             menuItem.setActionView(null);  
    71.         }  
    72.     };  
    73. }  


    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);

    效果图:

    代码:

    [java] view plaincopy
     
    1. package in.wptrafficanalyzer.actionbardropdownnavigation;  
    2.   
    3. import android.app.ActionBar;  
    4. import android.app.ActionBar.OnNavigationListener;  
    5. import android.app.Activity;  
    6. import android.os.Bundle;  
    7. import android.widget.ArrayAdapter;  
    8. import android.widget.Toast;  
    9.   
    10. public class MainActivity extends Activity {  
    11.   
    12.     /** An array of strings to populate dropdown list */  
    13.     String[] actions = new String[] { "Bookmark""Subscribe""Share" };  
    14.   
    15.     /** Called when the activity is first created. */  
    16.     @Override  
    17.     public void onCreate(Bundle savedInstanceState) {  
    18.         super.onCreate(savedInstanceState);  
    19.         setContentView(R.layout.main);  
    20.   
    21.         /** Create an array adapter to populate dropdownlist */  
    22.         ArrayAdapter<String> adapter = new ArrayAdapter<String>(  
    23.                 getBaseContext(),  
    24.                 android.R.layout.simple_spinner_dropdown_item, actions);  
    25.   
    26.         /** Enabling dropdown list navigation for the action bar */  
    27.         getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);  
    28.   
    29.         /** Defining Navigation listener */  
    30.         ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {  
    31.   
    32.             @Override  
    33.             public boolean onNavigationItemSelected(int itemPosition,  
    34.                     long itemId) {  
    35.                 Toast.makeText(getBaseContext(),  
    36.                         "You selected : " + actions[itemPosition],  
    37.                         Toast.LENGTH_SHORT).show();  
    38.                 return false;  
    39.             }  
    40.         };  
    41.   
    42.         /** 
    43.          * Setting dropdown items and item navigation listener for the actionbar 
    44.          */  
    45.         getActionBar().setListNavigationCallbacks(adapter, navigationListener);  
    46.     }  
    47. }  

    代码下载: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/ 文件夹中加入布局文件。

    代码:

    [java] view plaincopy
     
    1. package com.example.demo_actionbarcontextual;  
    2.   
    3. import com.example.demo_actionbarcontextual.R;  
    4. import android.app.Activity;  
    5. import android.os.Bundle;  
    6. import android.view.ActionMode;  
    7. import android.view.Menu;  
    8. import android.view.MenuInflater;  
    9. import android.view.MenuItem;  
    10. import android.view.View;  
    11. import android.widget.Toast;  
    12.   
    13. public class MainActivity extends Activity {  
    14.     protected Object mActionMode;  
    15.   
    16.     @Override  
    17.     public void onCreate(Bundle savedInstanceState) {  
    18.         super.onCreate(savedInstanceState);  
    19.         setContentView(R.layout.activity_main);  
    20.           
    21.         View view = findViewById(R.id.myView);  
    22.           
    23.         // 对EditText设置长点击事件,用来显示 contextual action bar  
    24.         view.setOnLongClickListener(new View.OnLongClickListener() {  
    25.             public boolean onLongClick(View view) {  
    26.                 if (mActionMode != null) {  
    27.                     return false;  
    28.                 }  
    29.   
    30.                 // Start the Contextual Action Bar using the ActionMode.Callback defined above  
    31.                 mActionMode = MainActivity.this  
    32.                         .startActionMode(mActionModeCallback);  
    33.                 view.setSelected(true);  
    34.                 return true;  
    35.             }  
    36.         });  
    37.     }  
    38.   
    39.     @Override  
    40.     public boolean onCreateOptionsMenu(Menu menu) {  
    41.         MenuInflater inflater = getMenuInflater();  
    42.         inflater.inflate(R.menu.mainmenu, menu);  
    43.         return true;  
    44.     }  
    45.   
    46.     @Override  
    47.     public boolean onOptionsItemSelected(MenuItem item) {  
    48.         Toast.makeText(this"Just a test", Toast.LENGTH_SHORT).show();  
    49.         return true;  
    50.     }  
    51.   
    52.     private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {  
    53.   
    54.         // Called when the action mode is created; startActionMode() was called  
    55.         public boolean onCreateActionMode(ActionMode mode, Menu menu) {  
    56.             // Inflate a menu resource providing context menu items  
    57.             MenuInflater inflater = mode.getMenuInflater();  
    58.             // R.menu.contextual 是 contextual action bar 的布局文件, 在 /res/menu/ 文件夹下  
    59.             inflater.inflate(R.menu.contextual, menu);  
    60.             return true;  
    61.         }  
    62.   
    63.         // Called each time the action mode is shown. Always called after  
    64.         // onCreateActionMode, but  
    65.         // may be called multiple times if the mode is invalidated.  
    66.         public boolean onPrepareActionMode(ActionMode mode, Menu menu) {  
    67.             return false// Return false if nothing is done  
    68.         }  
    69.   
    70.         // 当用户点击 contextual action bar 的 menu item 的时候产生点击事件  
    71.         public boolean onActionItemClicked(ActionMode mode, MenuItem item) {  
    72.             switch (item.getItemId()) {  
    73.             case R.id.toast:  
    74.                 Toast.makeText(MainActivity.this"Selected menu",  
    75.                         Toast.LENGTH_LONG).show();  
    76.                 mode.finish(); // 关闭 contextual action bar  
    77.                 return true;  
    78.             default:  
    79.                 return false;  
    80.             }  
    81.         }  
    82.   
    83.         // Called when the user exits the action mode  
    84.         public void onDestroyActionMode(ActionMode mode) {  
    85.             mActionMode = null;  
    86.         }  
    87.     };  
    88.   
    89. }  

    下载:Demo_ActionBarContextual


    Action bar 之 navigation tabs

    左图是竖直屏幕的效果,右图是横屏的效果:

      

    代码:

    [java] view plaincopy
     
    1. package de.arvidg.exampleactionbartabs;  
    2.   
    3. import android.app.ActionBar;  
    4. import android.app.ActionBar.Tab;  
    5. import android.app.Activity;  
    6. import android.app.Fragment;  
    7. import android.app.FragmentTransaction;  
    8. import android.content.Context;  
    9. import android.os.Bundle;  
    10. import android.util.Log;  
    11. import android.view.Menu;  
    12. import android.view.MenuInflater;  
    13. import android.view.MenuItem;  
    14. import android.widget.Toast;  
    15.   
    16. public class StartActivity extends Activity {  
    17.     public static Context appContext;  
    18.   
    19.     /** Called when the activity is first created. */  
    20.     @Override  
    21.     public void onCreate(Bundle savedInstanceState) {  
    22.         super.onCreate(savedInstanceState);  
    23.         setContentView(R.layout.main);  
    24.         appContext = getApplicationContext();  
    25.   
    26.         // ActionBar  
    27.         ActionBar actionbar = getActionBar();  
    28.         // 设置action bar 的 navigation mode   
    29.         actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);  
    30.   
    31.         // 添加 action bar 的 tabs  
    32.         ActionBar.Tab PlayerTab = actionbar.newTab().setText("Fragment A");  
    33.         ActionBar.Tab StationsTab = actionbar.newTab().setText("Fragment B");  
    34.   
    35.         // 实例化 fragment action bar 是用 fragment 来显示的  
    36.         Fragment PlayerFragment = new AFragment();  
    37.         Fragment StationsFragment = new BFragment();  
    38.   
    39.         // 对 tabs 设置监听事件  
    40.         PlayerTab.setTabListener(new MyTabsListener(PlayerFragment));  
    41.         StationsTab.setTabListener(new MyTabsListener(StationsFragment));  
    42.           
    43.         // 最后把 tabs 加入监听事件  
    44.         actionbar.addTab(PlayerTab);  
    45.         actionbar.addTab(StationsTab);  
    46.     }  
    47.   
    48.     @Override  
    49.     public boolean onCreateOptionsMenu(Menu menu) {  
    50.         MenuInflater inflater = getMenuInflater();  
    51.         inflater.inflate(R.menu.main, menu);  
    52.         return true;  
    53.     }  
    54.   
    55.     @Override  
    56.     public boolean onOptionsItemSelected(MenuItem item) {  
    57.         switch (item.getItemId()) {  
    58.         case R.id.menuitem_search:  
    59.             Toast.makeText(appContext, "search", Toast.LENGTH_SHORT).show();  
    60.             return true;  
    61.         case R.id.menuitem_add:  
    62.             Toast.makeText(appContext, "add", Toast.LENGTH_SHORT).show();  
    63.             return true;  
    64.         case R.id.menuitem_share:  
    65.             Toast.makeText(appContext, "share", Toast.LENGTH_SHORT).show();  
    66.             return true;  
    67.         case R.id.menuitem_feedback:  
    68.             Toast.makeText(appContext, "feedback", Toast.LENGTH_SHORT).show();  
    69.             return true;  
    70.         case R.id.menuitem_about:  
    71.             Toast.makeText(appContext, "about", Toast.LENGTH_SHORT).show();  
    72.             return true;  
    73.         case R.id.menuitem_quit:  
    74.             Toast.makeText(appContext, "quit", Toast.LENGTH_SHORT).show();  
    75.             return true;  
    76.         }  
    77.         return false;  
    78.     }  
    79.   
    80.     // @Override  
    81.     // protected void onSaveInstanceState(Bundle outState) {  
    82.     // super.onSaveInstanceState(outState);  
    83.     // outState.putInt("tab", getActionBar().getSelectedNavigationIndex());  
    84.     // }  
    85.   
    86. }  
    87.   
    88. // 实例化 tabs 的监听类   
    89. class MyTabsListener implements ActionBar.TabListener {  
    90.     public Fragment fragment;  
    91.       
    92.     // 传入监听的 tab 的 fragment  
    93.     public MyTabsListener(Fragment fragment) {  
    94.         this.fragment = fragment;  
    95.     }  
    96.   
    97.       
    98.     // 重复两次以上点击 tab  
    99.     @Override  
    100.     public void onTabReselected(Tab tab, FragmentTransaction ft) { // ft 用来控制 fragment  
    101.         Toast.makeText(StartActivity.appContext, "Reselected!",  
    102.                 Toast.LENGTH_SHORT).show();  
    103.     }  
    104.   
    105.     // 就点击一次   
    106.     @Override  
    107.     public void onTabSelected(Tab tab, FragmentTransaction ft) {  
    108.         ft.replace(R.id.fragment_container, fragment);  
    109.     }  
    110.       
    111.     // 不点击  
    112.     @Override  
    113.     public void onTabUnselected(Tab tab, FragmentTransaction ft) {  
    114.         ft.remove(fragment);  
    115.     }  


    Action bar 之 ShareActionProvider

    1.首先要在menu布局文件的item标签中加入
    android:actionProviderClass="android.widget.ShareActionProvider"
    2.得到ShareActionProvider的实例
    provider = (ShareActionProvider) menu.findItem(R.id.menu_share)
    .getActionProvider();
    3.设置Intent
    效果图:

    代码:

    [java] view plaincopy
     
    1. package com.example.demo_shareactionprovider;  
    2.   
    3. import android.os.Bundle;  
    4. import android.app.Activity;  
    5. import android.content.Intent;  
    6. import android.view.Menu;  
    7. import android.view.MenuItem;  
    8. import android.widget.ShareActionProvider;  
    9.   
    10. public class MainActivity extends Activity {  
    11.     ShareActionProvider provider = null;  
    12.   
    13.     @Override  
    14.     protected void onCreate(Bundle savedInstanceState) {  
    15.         super.onCreate(savedInstanceState);  
    16.         setContentView(R.layout.activity_main);  
    17.     }  
    18.   
    19.     @Override  
    20.     public boolean onCreateOptionsMenu(Menu menu) {  
    21.         // Inflate the menu; this adds items to the action bar if it is present.  
    22.         getMenuInflater().inflate(R.menu.activity_main, menu);  
    23.   
    24.           
    25.         // Get the ActionProvider         
    26.         provider = (ShareActionProvider) menu.findItem(R.id.menu_share)  
    27.                 .getActionProvider();  
    28.         // Initialize the share intent  
    29.         Intent intent = new Intent(Intent.ACTION_SEND);  
    30.         intent.setType("text/plain");  
    31.         intent.putExtra(Intent.EXTRA_TEXT, "Text I want to share");  
    32.         provider.setShareIntent(intent);  
    33.   
    34.         return true;  
    35.     }  
    36.       
    37.       
    38.   
    39.     @Override  
    40.     public boolean onOptionsItemSelected(MenuItem item) {  
    41.         switch (item.getItemId()) {  
    42.         case R.id.menu_share:  
    43.             break;  
    44.         default:  
    45.             break;  
    46.         }  
    47.         return super.onOptionsItemSelected(item);  
    48.     }  
    49. }  

    /res/menu/activity_main.xml


    [html] view plaincopy
     
    1. <menu xmlns:android="http://schemas.android.com/apk/res/android" >  
    2.   
    3.     <item  
    4.         android:id="@+id/menu_share"  
    5.         android:actionProviderClass="android.widget.ShareActionProvider"  
    6.         android:showAsAction="ifRoom"  
    7.         android:title="Share"/>  
    8.     <item  
    9.         android:id="@+id/item1"  
    10.         android:icon="@android:drawable/ic_menu_call"  
    11.         android:showAsAction="ifRoom"  
    12.         android:title="">  
    13.     </item>  
    14.   
    15. </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

    [html] view plaincopy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <resources>  
    3.     <!-- the theme applied to the application or activity -->  
    4.     <style name="CustomActivityTheme" parent="@android:style/Theme.Holo">  
    5.         <item name="android:actionBarTabTextStyle">@style/CustomTabTextStyle</item>  
    6.         <item name="android:actionBarDivider">@drawable/ab_divider</item>  
    7.         <item name="android:actionBarItemBackground">@drawable/ab_item_background</item>  
    8.     </style>  
    9.   
    10.     <!-- style for the action bar tab text -->  
    11.     <style name="CustomTabTextStyle" parent="@android:style/TextAppearance.Holo">  
    12.         <item name="android:textColor">#2456c2</item>  
    13.     </style>  
    14. </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/styles.xml

    https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/themes.xml

    例子1:怎么改变tab的字体大小、字体颜色等

    [html] view plaincopy
     
      1. <pre name="code" class="html">在themes.xml文件中关于tab的属性有:</pre><br>  
      2. <br>  
      3. <pre></pre>  
      4. <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>  
      5. android:actionBarTabTextStyle //Defines a style resource for text in the navigation tabs.  
      6. <p><br>  
      7. </p>  
      8. <p>在themes.xml文件中找到:</p>  
      9. <p></p>  
      10. <p></p>  
      11. <pre name="code" class="html"><itemnameitemname="actionBarTabTextStyle">@style/Widget.Holo.Light.ActionBar.TabText</item></pre>  
      12. <p></p>  
      13. <p>在style.xml文件中找到</p>  
      14. <pre name="code" class="html"><style name="Widget.Holo.Light.ActionBar.TabText" parent="Widget.Holo.ActionBar.TabText">  
      15. </pre>  
      16. <p><br>  
      17. </p>  
      18. 在style.xml文件中通过Widget.Holo.ActionBar.TabText,我们可以找到下面<br>  
      19. <pre name="code" class="html"><style name="Widget.Holo.ActionBar.TabText" parent="Widget.ActionBar.TabText">  
      20.     <item name="android:textAppearance">@style/TextAppearance.Holo.Medium</item>  
      21.     <item name="android:textColor">?android:attr/textColorPrimary</item>  
      22.     <item name="android:textSize">12sp</item>  
      23.     <item name="android:textStyle">bold</item>  
      24.     <item name="android:textAllCaps">true</item>  
      25.     <item name="android:ellipsize">marquee</item>  
      26.     <item name="android:maxLines">2</item>  
      27. </style>  
      28. </pre>  
      29. <p><br>  
      30. </p>  
      31. <p>下面是我们工程中styles.xml的内容:<br>  
      32. </p>  
      33. <pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>  
      34. <resources xmlns:android="http://schemas.android.com/apk/res/android">  
      35.   
      36.     <style name="CustomActivityThemo" parent="@android:style/Theme.Holo.Light">  
      37.         <item name="android:actionBarTabTextStyle">@style/CustomTabTextStyle</item>  
      38.     </style>  
      39.   
      40.     <style name="CustomTabTextStyle" parent="@android:style/Widget.ActionBar.TabText">  
      41.         <item name="android:textSize">25sp</item>  
      42.         <item name="android:textColor">#FF0000</item>  
      43.         <item name="android:textStyle">italic|bold</item>  
      44.     </style>  
      45.   
      46. </resources>  
      47. </pre><br>  
      48. 我们必须在manifest.xml文件中的activity标签中设置<br>  
      49. <br>  
      50. <p></p>  
      51. <pre name="code" class="html"><activity  
      52.              
      53. <span style="white-space:pre">  </span>android:theme="@style/CustomActivityThemo" ></pre><pre name="code" class="html"><span style="white-space:pre">    </span>....  
      54. </activity>  
      55. </pre>  
      56. <p></p>  
      57. <p><br>  
      58. </p>  
      59. 代码:Demo_ActionBarTabsColor<br>  
      60. <p><br>  
      61. </p>  
      62. <p>例子2:改变action bar中tab的indicator(下面的那条横线)的颜色<br>  
      63. styles.xml文件内容:</p>  
      64. <p><br>  
      65. </p>  
      66. <pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>  
      67. <resources xmlns:android="http://schemas.android.com/apk/res/android">  
      68.   
      69.     <!-- the theme applied to the application or activity -->  
      70.     <style name="CustomActivityTheme" parent="@android:style/Theme.Holo.Light">  
      71.         <item name="android:actionBarTabTextStyle">@style/CustomTabTextStyle</item>  
      72.         <item name="android:actionBarTabBarStyle">@android:color/holo_orange_dark</item>  
      73.         <item name="android:actionBarTabStyle">@style/ActionBarTabStyle.Example</item>  
      74.     </style>  
      75.   
      76.     <!-- style for the action bar tab text -->  
      77.     <style name="CustomTabTextStyle" parent="@android:style/TextAppearance.Holo">  
      78.         <item name="android:textColor">#2456c2</item>  
      79.     </style>  
      80.   
      81.     <style name="ActionBarTabStyle.Example" parent="@android:style/Widget.Holo.Light.ActionBar.TabView">  
      82.         <item name="android:background">@drawable/tab_indicator_ab_example</item>  
      83.     </style>  
      84.   
      85. </resources>  
      86. </pre><br>  
      87. <br>  
      88. <p></p>  
      89. <p>android:background这个属性定义了tab的显示风格,对应的是一个xml文件不是图片<br>  
      90. </p>  
      91. <p></p>  
      92. <p><br>  
      93. </p>  
      94. <p><br>  
      95. </p>  
      96. <p>tab_indicator_ab_example.xml文件的内容:</p>  
      97. <p></p>  
      98. <pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>  
      99. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
      100.   
      101.     <!-- Non focused states -->  
      102.     <item android:drawable="@android:color/transparent" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>  
      103.     <item android:drawable="@drawable/tab_selected_example" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>  
      104.   
      105.     <!-- Focused states -->  
      106.     <item android:drawable="@drawable/tab_unselected_focused_example" android:state_focused="true" android:state_pressed="false" android:state_selected="false"/>  
      107.     <item android:drawable="@drawable/tab_selected_focused_example" android:state_focused="true" android:state_pressed="false" android:state_selected="true"/>  
      108.   
      109.     <!-- Pressed -->  
      110.     <!-- Non focused states -->  
      111.     <item android:drawable="@drawable/tab_unselected_pressed_example" android:state_focused="false" android:state_pressed="true" android:state_selected="false"/>  
      112.     <item android:drawable="@drawable/tab_selected_pressed_example" android:state_focused="false" android:state_pressed="true" android:state_selected="true"/>  
      113.   
      114.     <!-- Focused states -->  
      115.     <item android:drawable="@drawable/tab_unselected_pressed_example" android:state_focused="true" android:state_pressed="true" android:state_selected="false"/>  
      116.     <item android:drawable="@drawable/tab_selected_pressed_example" android:state_focused="true" android:state_pressed="true" android:state_selected="true"/>  
      117.   
      118. </selector>  
      119. </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>  
  • 相关阅读:
    php解决前端调接口跨域问题
    降低token 被盗风险安全问题
    u盘怎么解除写保护状态,u盘写保护怎么去掉
    安装vsftpd时出现错误
    对于vim 编译器的设置
    Vim 怎么设置显示行号,永久性显示行号
    Ubuntu系统设置过程中信息显示不全
    控制文件夹名显示的长短
    linux中好玩的游戏
    安装VMware Tools
  • 原文地址:https://www.cnblogs.com/YangBinChina/p/3559213.html
Copyright © 2011-2022 走看看