一、自定义MenuItem的视图
custom_view.xml (就是一个单选按钮)
xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android&#61;"http://schemas.android.com/apk/res/android"android:layout_width&#61;"wrap_content"android:layout_height&#61;"fill_parent"android:gravity&#61;"left|center_vertical"android:orientation&#61;"horizontal"><RadioGroupandroid:id&#61;"&#64;&#43;id/radio_nav"android:orientation&#61;"horizontal"android:layout_width&#61;"fill_parent"android:layout_height&#61;"wrap_content"><RadioButtonandroid:text&#61;"Custom"android:layout_width&#61;"wrap_content"android:layout_height&#61;"wrap_content"android:textColor&#61;"#ffffff"/><RadioButtonandroid:text&#61;"View!"android:layout_width&#61;"wrap_content"android:layout_height&#61;"wrap_content"android:textColor&#61;"#ffffff"/>RadioGroup>
LinearLayout>
MainActivity.java
//Inflate the custom viewView customNav &#61; LayoutInflater.from(this).inflate(R.layout.custom_view, null);//Bind to its state change((RadioGroup)customNav.findViewById(R.id.radio_nav)).setOnCheckedChangeListener(new OnCheckedChangeListener() {&#64;Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {Toast.makeText(MainActivity.this, "Navigation selection changed.", Toast.LENGTH_SHORT).show();}});//Attach to the action bar
getSupportActionBar().setCustomView(customNav);getSupportActionBar().setDisplayShowCustomEnabled(true);
二、添加圆形模糊进度的进度条
package com.kale.actionbar02;import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;public class MainActivity extends ActionBarActivity {&#64;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);setContentView(R.layout.activity_main);//设置actionbar上面显示进度条&#xff0c;true表示显示&#xff0c;如果是false表示不显示 setSupportProgressBarIndeterminateVisibility(true);//Attach to the action bar
getSupportActionBar().setCustomView(customNav);getSupportActionBar().setDisplayShowCustomEnabled(true);}
}
三、添加有进度的横向进度条
package com.kale.actionbar02;import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.Window;public class MainActivity extends ActionBarActivity {private int mProgress &#61; 100;Handler mHandler &#61; new Handler();Runnable mProgressRunner &#61; new Runnable() {&#64;Overridepublic void run() {mProgress &#43;&#61; 2;// Normalize our progress along the progress bar&#39;s scaleint progress &#61; (Window.PROGRESS_END - Window.PROGRESS_START) / 100* mProgress;setSupportProgress(progress);if (mProgress <100) {mHandler.postDelayed(mProgressRunner, 50);}}};&#64;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); supportRequestWindowFeature(Window.FEATURE_PROGRESS);setContentView(R.layout.activity_main);// 设置actionbar上面显示进度条&#xff0c;true表示显示&#xff0c;如果是false表示不显示//setSupportProgressBarVisibility(true);//设置初始状态是否显示进度条&#xff0c;一般我们不显示。在用的时候再显示它
findViewById(R.id.button_id).setOnClickListener(new View.OnClickListener() {&#64;Overridepublic void onClick(View arg0) {if (mProgress &#61;&#61; 100) {mProgress &#61; 0;mProgressRunner.run();}}});}}
四、添加下拉导航&#43;悬浮模式
给ActionBar添加下拉导航的方法都是添加一个适配器&#xff0c;可以使简单的ArrayAdapter&#xff0c;也可以使SpinnerAdapter&#xff0c;添加完数据后再绑定个监听器。其实和Spinner没啥太大的区别。悬浮模式是可以让布局文件从ActionBar底下通过&#xff0c;可以实现透明效果。其代码就一行&#xff1a;
// 设置ActionBar为悬浮的&#xff0c;就是说悬浮在布局文件上supportRequestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
注意看一下布局的&#xff0c;上边距。是用的ActionBar的高度来指定的
xml version&#61;"1.0" encoding&#61;"utf-8"?><FrameLayout xmlns:android&#61;"http://schemas.android.com/apk/res/android"android:layout_width&#61;"fill_parent"android:layout_height&#61;"fill_parent"><ScrollViewandroid:layout_width&#61;"fill_parent"android:layout_height&#61;"fill_parent"><LinearLayoutandroid:layout_width&#61;"fill_parent"android:layout_height&#61;"wrap_content"android:paddingLeft&#61;"10dp"android:paddingRight&#61;"10dp"android:paddingTop&#61;"?actionBarSize"android:orientation&#61;"vertical"><TextViewandroid:id&#61;"&#64;&#43;id/bunch_of_text"android:layout_width&#61;"fill_parent"android:layout_height&#61;"wrap_content" />LinearLayout>ScrollView>
FrameLayout>
下面是下拉导航的代码&#xff1a;
package com.kale.actionbar02;import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.OnNavigationListener;
import android.support.v7.app.ActionBarActivity;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;public class MainActivity extends ActionBarActivity {&#64;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 设置ActionBar为悬浮的&#xff0c;就是说悬浮在布局文件上
supportRequestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);setContentView(R.layout.activity_main);// 将ActionBar的操作模型设置为NAVIGATION_MODE_LIST
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);getSupportActionBar().setTitle(null);Context context &#61; getSupportActionBar().getThemedContext();ArrayAdapter
getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.bar_color));TextView bunchOfText &#61; (TextView) findViewById(R.id.bunch_of_text);bunchOfText.setText(builder.toString());}/*** 实现 ActionBar.OnNavigationListener接口*/class DropDownListenser implements OnNavigationListener {// 得到和Adapter里一致的字符数组String[] listNames &#61; getResources().getStringArray(R.array.locations);&#64;Overridepublic boolean onNavigationItemSelected(int itemPosition, long itemId) {Toast.makeText(getApplicationContext(), listNames[itemPosition], 0).show();return false;}}}
values/array.xml
xml version&#61;"1.0" encoding&#61;"utf-8"?>
<resources><string-array name&#61;"locations"><item>Homeitem><item>Emailitem><item>Calendaritem><item>Browseritem><item>Clockitem>string-array>
resources>
全部的代码&#xff1a;
package com.kale.actionbar02;import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.OnNavigationListener;
import android.support.v7.app.ActionBarActivity;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;public class MainActivity extends ActionBarActivity {&#64;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 设置ActionBar为悬浮的&#xff0c;就是说悬浮在布局文件上
supportRequestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);setContentView(R.layout.activity_main);// 将ActionBar的操作模型设置为NAVIGATION_MODE_LIST
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);getSupportActionBar().setTitle(null);Context context &#61; getSupportActionBar().getThemedContext();ArrayAdapter
getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.bar_color));StringBuilder builder &#61; new StringBuilder();for (int i &#61; 0; i <3; i&#43;&#43;) {for (String dialog : DIALOGUE) {builder.append(dialog).append("\n\n");}}TextView bunchOfText &#61; (TextView) findViewById(R.id.bunch_of_text);bunchOfText.setText(builder.toString());}/*** 实现 ActionBar.OnNavigationListener接口*/class DropDownListenser implements OnNavigationListener {// 得到和Adapter里一致的字符数组String[] listNames &#61; getResources().getStringArray(R.array.locations);&#64;Overridepublic boolean onNavigationItemSelected(int itemPosition, long itemId) {Toast.makeText(getApplicationContext(), listNames[itemPosition], 0).show();return false;}}public static final String[] DIALOGUE &#61; new String[] {"So shaken as we are, so wan with care,"&#43; "Find we a time for frighted peace to pant,"&#43; "And breathe short-winded accents of new broils"&#43; "To be commenced in strands afar remote."&#43; "No more the thirsty entrance of this soil"&#43; "Shall daub her lips with her own children&#39;s blood;"&#43; "Nor more shall trenching war channel her fields,"&#43; "Nor bruise her flowerets with the armed hoofs"&#43; "Of hostile paces: those opposed eyes,"&#43; "Which, like the meteors of a troubled heaven,"&#43; "All of one nature, of one substance bred,"&#43; "Did lately meet in the intestine shock"&#43; "And furious close of civil butchery"&#43; "Shall now, in mutual well-beseeming ranks,"&#43; "March all one way and be no more opposed"&#43; "Against acquaintance, kindred and allies:"&#43; "The edge of war, like an ill-sheathed knife,"&#43; "No more shall cut his master. Therefore, friends,"&#43; "As far as to the sepulchre of Christ,"&#43; "Whose soldier now, under whose blessed cross"&#43; "We are impressed and engaged to fight,"&#43; "Forthwith a power of English shall we levy;"&#43; "Whose arms were moulded in their mothers&#39; womb"&#43; "To chase these pagans in those holy fields"&#43; "Over whose acres walk&#39;d those blessed feet"&#43; "Which fourteen hundred years ago were nail&#39;d"&#43; "For our advantage on the bitter cross."&#43; "But this our purpose now is twelve month old,"&#43; "And bootless &#39;tis to tell you we will go:"&#43; "Therefore we meet not now. Then let me hear"&#43; "Of you, my gentle cousin Westmoreland,"&#43; "What yesternight our council did decree"&#43; "In forwarding this dear expedience.","Hear him but reason in divinity,"&#43; "And all-admiring with an inward wish"&#43; "You would desire the king were made a prelate:"&#43; "Hear him debate of commonwealth affairs,"&#43; "You would say it hath been all in all his study:"&#43; "List his discourse of war, and you shall hear"&#43; "A fearful battle render&#39;d you in music:"&#43; "Turn him to any cause of policy,"&#43; "The Gordian knot of it he will unloose,"&#43; "Familiar as his garter: that, when he speaks,"&#43; "The air, a charter&#39;d libertine, is still,"&#43; "And the mute wonder lurketh in men&#39;s ears,"&#43; "To steal his sweet and honey&#39;d sentences;"&#43; "So that the art and practic part of life"&#43; "Must be the mistress to this theoric:"&#43; "Which is a wonder how his grace should glean it,"&#43; "Since his addiction was to courses vain,"&#43; "His companies unletter&#39;d, rude and shallow,"&#43; "His hours fill&#39;d up with riots, banquets, sports,"&#43; "And never noted in him any study,"&#43; "Any retirement, any sequestration"&#43; "From open haunts and popularity.","I come no more to make you laugh: things now,"&#43; "That bear a weighty and a serious brow,"&#43; "Sad, high, and working, full of state and woe,"&#43; "Such noble scenes as draw the eye to flow,"&#43; "We now present. Those that can pity, here"&#43; "May, if they think it well, let fall a tear;"&#43; "The subject will deserve it. Such as give"&#43; "Their money out of hope they may believe,"&#43; "May here find truth too. Those that come to see"&#43; "Only a show or two, and so agree"&#43; "The play may pass, if they be still and willing,"&#43; "I&#39;ll undertake may see away their shilling"&#43; "Richly in two short hours. Only they"&#43; "That come to hear a merry bawdy play,"&#43; "A noise of targets, or to see a fellow"&#43; "In a long motley coat guarded with yellow,"&#43; "Will be deceived; for, gentle hearers, know,"&#43; "To rank our chosen truth with such a show"&#43; "As fool and fight is, beside forfeiting"&#43; "Our own brains, and the opinion that we bring,"&#43; "To make that only true we now intend,"&#43; "Will leave us never an understanding friend."&#43; "Therefore, for goodness&#39; sake, and as you are known"&#43; "The first and happiest hearers of the town,"&#43; "Be sad, as we would make ye: think ye see"&#43; "The very persons of our noble story"&#43; "As they were living; think you see them great,"&#43; "And follow&#39;d with the general throng and sweat"&#43; "Of thousand friends; then in a moment, see"&#43; "How soon this mightiness meets misery:"&#43; "And, if you can be merry then, I&#39;ll say"&#43; "A man may weep upon his wedding-day.","First, heaven be the record to my speech!"&#43; "In the devotion of a subject&#39;s love,"&#43; "Tendering the precious safety of my prince,"&#43; "And free from other misbegotten hate,"&#43; "Come I appellant to this princely presence."&#43; "Now, Thomas Mowbray, do I turn to thee,"&#43; "And mark my greeting well; for what I speak"&#43; "My body shall make good upon this earth,"&#43; "Or my divine soul answer it in heaven."&#43; "Thou art a traitor and a miscreant,"&#43; "Too good to be so and too bad to live,"&#43; "Since the more fair and crystal is the sky,"&#43; "The uglier seem the clouds that in it fly."&#43; "Once more, the more to aggravate the note,"&#43; "With a foul traitor&#39;s name stuff I thy throat;"&#43; "And wish, so please my sovereign, ere I move,"&#43; "What my tongue speaks my right drawn sword may prove.","Now is the winter of our discontent"&#43; "Made glorious summer by this sun of York;"&#43; "And all the clouds that lour&#39;d upon our house"&#43; "In the deep bosom of the ocean buried."&#43; "Now are our brows bound with victorious wreaths;"&#43; "Our bruised arms hung up for monuments;"&#43; "Our stern alarums changed to merry meetings,"&#43; "Our dreadful marches to delightful measures."&#43; "Grim-visaged war hath smooth&#39;d his wrinkled front;"&#43; "And now, instead of mounting barded steeds"&#43; "To fright the souls of fearful adversaries,"&#43; "He capers nimbly in a lady&#39;s chamber"&#43; "To the lascivious pleasing of a lute."&#43; "But I, that am not shaped for sportive tricks,"&#43; "Nor made to court an amorous looking-glass;"&#43; "I, that am rudely stamp&#39;d, and want love&#39;s majesty"&#43; "To strut before a wanton ambling nymph;"&#43; "I, that am curtail&#39;d of this fair proportion,"&#43; "Cheated of feature by dissembling nature,"&#43; "Deformed, unfinish&#39;d, sent before my time"&#43; "Into this breathing world, scarce half made up,"&#43; "And that so lamely and unfashionable"&#43; "That dogs bark at me as I halt by them;"&#43; "Why, I, in this weak piping time of peace,"&#43; "Have no delight to pass away the time,"&#43; "Unless to spy my shadow in the sun"&#43; "And descant on mine own deformity:"&#43; "And therefore, since I cannot prove a lover,"&#43; "To entertain these fair well-spoken days,"&#43; "I am determined to prove a villain"&#43; "And hate the idle pleasures of these days."&#43; "Plots have I laid, inductions dangerous,"&#43; "By drunken prophecies, libels and dreams,"&#43; "To set my brother Clarence and the king"&#43; "In deadly hate the one against the other:"&#43; "And if King Edward be as true and just"&#43; "As I am subtle, false and treacherous,"&#43; "This day should Clarence closely be mew&#39;d up,"&#43; "About a prophecy, which says that &#39;G&#39;"&#43; "Of Edward&#39;s heirs the murderer shall be."&#43; "Dive, thoughts, down to my soul: here"&#43; "Clarence comes.","To bait fish withal: if it will feed nothing else,"&#43; "it will feed my revenge. He hath disgraced me, and"&#43; "hindered me half a million; laughed at my losses,"&#43; "mocked at my gains, scorned my nation, thwarted my"&#43; "bargains, cooled my friends, heated mine"&#43; "enemies; and what&#39;s his reason? I am a Jew. Hath"&#43; "not a Jew eyes? hath not a Jew hands, organs,"&#43; "dimensions, senses, affections, passions? fed with"&#43; "the same food, hurt with the same weapons, subject"&#43; "to the same diseases, healed by the same means,"&#43; "warmed and cooled by the same winter and summer, as"&#43; "a Christian is? If you prick us, do we not bleed?"&#43; "if you tickle us, do we not laugh? if you poison"&#43; "us, do we not die? and if you wrong us, shall we not"&#43; "revenge? If we are like you in the rest, we will"&#43; "resemble you in that. If a Jew wrong a Christian,"&#43; "what is his humility? Revenge. If a Christian"&#43; "wrong a Jew, what should his sufferance be by"&#43; "Christian example? Why, revenge. The villany you"&#43; "teach me, I will execute, and it shall go hard but I"&#43; "will better the instruction.","Virtue! a fig! &#39;tis in ourselves that we are thus"&#43; "or thus. Our bodies are our gardens, to the which"&#43; "our wills are gardeners: so that if we will plant"&#43; "nettles, or sow lettuce, set hyssop and weed up"&#43; "thyme, supply it with one gender of herbs, or"&#43; "distract it with many, either to have it sterile"&#43; "with idleness, or manured with industry, why, the"&#43; "power and corrigible authority of this lies in our"&#43; "wills. If the balance of our lives had not one"&#43; "scale of reason to poise another of sensuality, the"&#43; "blood and baseness of our natures would conduct us"&#43; "to most preposterous conclusions: but we have"&#43; "reason to cool our raging motions, our carnal"&#43; "stings, our unbitted lusts, whereof I take this that"&#43; "you call love to be a sect or scion.","Blow, winds, and crack your cheeks! rage! blow!"&#43; "You cataracts and hurricanoes, spout"&#43; "Till you have drench&#39;d our steeples, drown&#39;d the cocks!"&#43; "You sulphurous and thought-executing fires,"&#43; "Vaunt-couriers to oak-cleaving thunderbolts,"&#43; "Singe my white head! And thou, all-shaking thunder,"&#43; "Smite flat the thick rotundity o&#39; the world!"&#43; "Crack nature&#39;s moulds, an germens spill at once,"&#43; "That make ingrateful man!" };}
源码下载&#xff1a;http://download.csdn.net/detail/shark0017/7688143