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的例子:
- 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>
Android ActionBar 一步一步分析 (转)的更多相关文章
- 一步一步了解Cocos2dx 3.0 正式版本开发环境搭建(Win32/Android)
cocos2d-x 3.0发布有一段时间了,作为一个初学者,我一直觉得cocos2d-x很坑.每个比较大的版本变动,都会有不一样的项目创建方式,每次的跨度都挺大…… 但是凭心而论,3.0RC版本开始 ...
- 一步一步学android控件(之十五) —— DegitalClock & AnalogClock
原本计划DigitalClock和AnalogClock单独各一篇来写,但是想想,两个控件的作用都一样,就和在一起写一篇了. DegitalClock和AnalogClock控件主要用于显示当前时间信 ...
- 一步一步学android控件(之十六)—— CheckBox
根据使用场景不同,有时候使用系统默认的CheckBox样式就可以了,但是有时候就需要自定义CheckBox的样式.今天主要学习如何自定义CheckBox样式.在CheckBox状态改变时有时需要做一些 ...
- Android 从硬件到应用程序:一步一步爬上去 6 -- 我写的APP测试框架层硬件服务(终点)
创Android Applicationproject:采用Eclipse的Android插入ADT创Androidproject,project名字Gpio,创建完成后,project文件夹pack ...
- 一步一步学android控件(之六) —— MultiAutoCompleteTextView
今天学习的控件是MultiAutoCompleteTextView . 提到MultiAutoCompleteTextView 我们就自然而然地想到AutoCompleteTextView ,就想知道 ...
- Ace教你一步一步做Android新闻客户端(一)
复制粘贴了那么多博文很不好意思没点自己原创的也说不出去,现在写一篇一步一步教你做安卓新闻客户端,借此机会也是让自己把相关的技术再复习一遍,大神莫笑,专门做给新手看. 手里存了两篇,一个包括软件视图 和 ...
- android一步一步实现视频clientapp(一)
我开发完毕了一个完整的视频clientapp.如今.分享出来.供刚開始学习的人学习參考(大神就不用看了,比較简单,仅供入门),大家相互交流相互学习. 项目有些功能,我时间也不是非常多.仅仅能时不时更新 ...
- 一步一步教你在 Android 里创建自己的账号系统(一)
大家假设喜欢我的博客,请关注一下我的微博,请点击这里(http://weibo.com/kifile),谢谢 转载请标明出处(http://blog.csdn.net/kifile),再次感谢 大家在 ...
- Android 从硬件到应用:一步一步向上爬 4 -- 使用 JNI 方法调硬件驱动
Android下,java应用程序通过JNI方法调用硬件抽象层模块,在Android 从硬件到应用:一步一步向上爬 3 -- 硬件抽象层訪问硬件驱动 中我们已经编译好了硬件抽象层模块,以下就要開始为H ...
- 一步一步pwn路由器之wr940栈溢出漏洞分析与利用
前言 本文由 本人 首发于 先知安全技术社区: https://xianzhi.aliyun.com/forum/user/5274 这个是最近爆出来的漏洞,漏洞编号:CVE-2017-13772 固 ...
随机推荐
- 获取JDBC中的ResultSet的记录的条数
方法一:利用ResultSet的getRow方法来获得ResultSet的总行数 Java代码 ResultSet rs; rs.last(); //移到最后一行 int rowCount = rs. ...
- SQL 两种表复制语句
1.INSERT INTO SELECT语句 语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Tab ...
- ios框架中UIResponder的职责链设计模式应用
今天有空,就把UIResponder的职责链图上传一下 如果不理解职责链设计模式的朋友,请参考:http://www.cnblogs.com/langtianya/p/4060941.html
- block与函数指针有什么区别
block就是一个代码块,但是它的神奇之处在于在内联(inline)执行的时候(这和C++很像)还可以传递参数.同时block本身也可以被作为参数在方法和函数间传递,这就给予了block无限的可能. ...
- 用python写一个hello world程序
1,http://www.python.org/download/ 下载windows安装包, 2,python环境变量配置 (1)设置环境变量:我的电脑-右键-属性-高级-环境变量 在Path中加入 ...
- 关于Java类加载双亲委派机制的思考(附一道面试题)
预定义类加载器和双亲委派机制 JVM预定义的三种类型类加载器: 启动(Bootstrap)类加载器:是用本地代码实现的类装入器,它负责将 <Java_Runtime_Home>/lib下面 ...
- 调用gluNurbsCurve绘制圆弧
<OpenGL编程指南>第12章第3小结专门介绍调用GLU绘制NURBS曲线或曲面,很可惜的是并未给出绘制圆弧的例子.网上可以找到很多绘制整个园的例子,却没圆弧例子,自己瞎折腾了2个礼拜, ...
- Ultra-QuickSort
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 44489 Accepted: 16176 ...
- mac os 安装 pkg-config
wget http://pkgconfig.freedesktop.org/releases/pkg-config-0.29.tar.gz . env LDFLAGS="-framework ...
- ubuntu创建文件夹快捷方式命令
sudo ln -sT /path/to/source/directory /path/to/destination/directory 注意路径一定要用 绝对路径 例: /site-package ...