方法1.
<activity android:theme="@android:style/Theme.Holo.NoActionBar">方法2.
ActionBar actionBar = getActionBar(); actionBar.hide();
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.wind.actionbar" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name="com.wind.actionbar.HelloActionBarActivity" android:label="@string/app_name"> <!-- android:uiOptions="splitActionBarWhenNarrow" will display below --> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
package com.wind.actionbar; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.Menu; public class HelloActionBarActivity extends Activity { private static final String TAG = "HelloActionBarActivity"; //private ActionBar actionBar; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.v(TAG, "onCreate"); setContentView(R.layout.main); } @Override public boolean onCreateOptionsMenu(Menu menu) { this.getMenuInflater().inflate(R.menu.menu, menu); return super.onCreateOptionsMenu(menu); } }
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/item1" android:title="111" android:icon="@drawable/ic_launcher"> </item> <item android:id="@+id/item2" android:title="222" android:icon="@drawable/ic_launcher"> </item> <item android:id="@+id/item3" android:title="333" android:icon="@drawable/ic_launcher"> </item> <item android:id="@+id/item4" android:title="444" android:icon="@drawable/ic_launcher"> </item> </menu>
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/item1" android:title="111" android:icon="@drawable/ic_launcher" android:showAsAction="ifRoom|withText" android:orderInCategory="1"> </item> <item android:id="@+id/item2" android:title="222" android:icon="@drawable/ic_launcher" android:showAsAction="ifRoom|withText" android:orderInCategory="2"> </item> <item android:id="@+id/item3" android:title="333" android:icon="@drawable/ic_launcher" android:showAsAction="ifRoom|withText" android:orderInCategory="3"> </item> <item android:id="@+id/item4" android:title="444" android:icon="@drawable/ic_launcher" android:showAsAction="ifRoom|withText" android:orderInCategory="0"> </item> </menu>
package com.wind.actionbar; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; public class HelloActionBarActivity extends Activity { private static final String TAG = "HelloActionBarActivity"; //private ActionBar actionBar; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.v(TAG, "onCreate"); setContentView(R.layout.main); } @Override public boolean onCreateOptionsMenu(Menu menu) { this.getMenuInflater().inflate(R.menu.menu, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { //响应menu item或者action item的点击 Log.v(TAG, "onOptionsItemSelected"); switch(item.getItemId()){ case R.id.item1: Log.v(TAG, "item1 pressed"); break; case R.id.item2: Log.v(TAG, "item2 pressed"); break; default: Log.v(TAG, "default"); } return super.onOptionsItemSelected(item); } }
7. Android4.0之后增加了分割actionbar的属性:
android:uiOptions="splitActionBarWhenNarrow在application或者Activity里添加这个属性,则会使Action item显示在屏幕的底部
如果你要使用应用图标来导航,要注意从Android4.0(API 级别 14)开始,必须通过调用setHomeButtonEnabled(true)方法来激活
ActionBar actionBar = this.getActionBar(); actionBar.setHomeButtonEnabled(true);
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: // 在action bar点击app icon; //TODO click event implement return true; ... } }
ActionBar actionBar = this.getActionBar(); actionBar.setDisplayShowHomeEnabled(false); //不显示app图标 actionBar.setDisplayShowTitleEnabled(false); //不显示Title
等价于:
ActionBar actionBar = this.getActionBar(); actionBar.setDisplayOptions(0,ActionBar.DISPLAY_SHOW_HOME|ActionBar.DISPLAY_SHOW_TITLE );
ActionBar actionBar = this.getActionBar(); actionBar.SetDisplayHomeAsUpEnabledtrue(true); //来激活应用程序向上导航功能
package com.wind.actionbar; import android.app.ActionBar; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.MenuItem; public class B_Activity extends Activity{ private static final String TAG = "B_Activity"; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.title); ActionBar actionBar = getActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); //激活应用程序向上导航 } @Override public boolean onOptionsItemSelected(MenuItem item) { Log.v(TAG, "onOptionsItemSelected"); switch (item.getItemId()) { case android.R.id.home: Log.v(TAG, "app icon is pressed"); finish(); break; default: break; } return super.onOptionsItemSelected(item); } }