Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android ActionBar(官方指導)

android ActionBar(官方指導)

編輯:關於Android編程

安裝ActionBar:

Support Android 3.0 and Above Only(僅支持3.0版本之後)


	    
	    ...
	

Support Android 2.1 and Above(如果想讓應用支持3.1版本之後,需如下操作)
1安裝v7 appcompat 庫
2Update your activity so that it extends ActionBarActivity. For example:
public class MainActivity extends ActionBarActivity { ... }
3In your manifest file, update either the element or individual elements to use one of the Theme.AppCompat themes. For example:

Adding Action Buttons(增加ActionBar):
1.Specify the Actions in XML

3.0以上版本:

	
		    
		    
		    
		    
			


2.0以上版本:

	
		    
		    
			    ...
			

2.Add the Actions to the Action Bar:使ActionBar生效

@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    // Inflate the menu items for use in the action bar
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.main_activity_actions, menu);
	    return super.onCreateOptionsMenu(menu);
	}

3.Respond to Action Buttons相應ActionBar的事件

@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle presses on the action bar items
	    switch (item.getItemId()) {
	        case R.id.action_search:
	            openSearch();
	            return true;
	        case R.id.action_settings:
	            openSettings();
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}

Add Up Button for Low-level Activities:增加返回至上一個Activity的動作按鈕:
1.不管是4.0以上版本還是使用ActionBarActivity從支持庫都需要:

增加android:parentActivityName屬性

	    ...
	    
	    
	        ...
	    
	    
	    
	        
	        
	    
	

調用setDisplayHomeAsUpEnabled():
@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_displaymessage);

	    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
	    // If your minSdkVersion is 11 or higher, instead use:
	    // getActionBar().setDisplayHomeAsUpEnabled(true);
	}


因為設置了android:parentActivityName屬性,所以系統知道要返回的父Activity,所以不用設置事件監聽


Styling the Action Bar(設置ActionBar風格):
Use an Android Theme(當使用支持庫的時候分別為如下,對應括號中為非支持庫的)
When using the Support Library, you must instead use the Theme.AppCompat themes:
Theme.AppCompat for the "dark" theme. (Theme.Holo)
Theme.AppCompat.Light for the "light" theme.(Theme.Holo.Light)
Theme.AppCompat.Light.DarkActionBar for the light theme with a dark action bar.(Theme.Holo.Light.DarkActionBar)

Customize the Background(改變ActionBar的背景)

For Android 3.0 and higher only:

1.定義資源文件:res/values/themes.xml

                        
			
			    
			    

			    
			    
			

2.Then apply your theme to your entire app or individual activities(引用資源文件):

For Android 2.1 and higher:
1.res/values/themes.xml:
                        
			    
			    

			    
			    
			

2.Then apply your theme to your entire app or individual activities:

Customize the Text Color(自定義ActionBar字體顏色)
For Android 3.0 and higher only:

res/values/themes.xml:

			
			
			    
			    

			    
			    

			    
			    

			    
			    
			

For Android 2.1 and higher(When using the Support Library, your style XML file might look like this:):

res/values/themes.xml

                        
			
			    
			    

			    
			    

			    
			    

			    
			    
			


Customize the Tab Indicator(自定義選項卡TabHost的樣式,使用選擇器)

res/drawable/actionbar_tab_indicator.xml:

                
		

		

		    
		    
		    

		    
		    
		    


		

		    
		    
		    

		    
		    
		    
		

然後:
For Android 3.0 and higher only:

res/values/themes.xml:

			
			
			    
			    

			    
			    
			

For Android 2.1 and higher:

res/values/themes.xml:

			
			
			    
			    

			    
			    
			

Overlaying the Action Bar(自動隱藏ActionBar):
Enable Overlay Mode(自動隱藏模式):

For Android 3.0 and higher only:

                        
			    
			    
			

Then apply your theme to your entire app or individual activities:

For Android 2.1 and higher:

                        
			    
			    
			

Then apply your theme to your entire app or individual activities:

Specify Layout Top-margin(top留空白,避免與ActionBar接觸):

沒有使用支持庫:

                
		    ...
		

使用了支持庫:

                
		
		    ...
		


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved