標籤:action bar android應用 android開發 android studio
Android官方入門文檔[6]添加Action按鈕 Adding Action Buttons
添加Action按鈕
This lesson teaches you to
1.Specify the Actions in XML
2.Add the Actions to the Action Bar
3.Respond to Action Buttons
4.Add Up Button for Low-level Activities
You should also read
?Providing Up Navigation
這節課教你
1.指定XML中的操作
2.給操作欄添加Action
3.響應Action按鈕
4.為低層級的活動添加向上Up按鈕
你也應該閱讀
?提供向上導航
The action bar allows you to add buttons for the most important action items relating to the app‘s current context. Those that appear directly in the action bar with an icon and/or text are known as action buttons. Actions that can‘t fit in the action bar or aren‘t important enough are hidden in the action overflow.
操作欄允許您給應用程式的當前環境中最重要的行動項目添加的按鈕。那些直接顯示在操作欄中有一個表徵圖和/或文字被稱為操作按鈕。Action在操作欄不適合或在Action溢出中不足夠重要會被隱藏。
Figure 1. An action bar with an action button for Search and the action overflow, which reveals additional actions.
圖1.與操作鍵搜尋和action溢出,從而揭示更多動作一個動作吧。
Specify the Actions in XML
指定XML的操作
--------------------------------------------------------------------------------
All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project‘s res/menu/ directory.
所有操作按鈕,並在action溢出可用其他的資料項目都以XML菜單資源中定義。將action動作添加到動作欄,建立在項目的res/menu/目錄中一個新的XML檔案。
Add an <item> element for each item you want to include in the action bar. For example:
添加<item>元素為你想要的操作欄包括每個項目。例如:
res/menu/main_activity_actions.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:showAsAction="never" />
</menu>
Download action bar icons
下載操作欄表徵圖
To best match the Android iconography guidelines, you should use icons provided in the Action Bar Icon Pack.
為了更好地配合了Android意象的指引,你應該使用在操作欄表徵圖包提供的表徵圖。
This declares that the Search action should appear as an action button when room is available in the action bar, but the Settings action should always appear in the overflow. (By default, all actions appear in the overflow, but it‘s good practice to explicitly declare your design intentions for each action.)
這宣告了搜尋action應顯示為一個動作按鈕,當房間在操作欄可用,但設定action應始終出現在溢出。 (預設情況下,所有動作出現溢出,但它是很好的做法,明確聲明你的設計意圖的每個action動作。)
The icon attribute requires a resource ID for an image. The name that follows @drawable/ must be the name of a bitmap image you‘ve saved in your project‘s res/drawable/ directory. For example, "@drawable/ic_action_search" refers to ic_action_search.png. Likewise, the title attribute uses a string resource that‘s defined by an XML file in your project‘s res/values/ directory, as discussed in Building a Simple User Interface.
表徵圖屬性要求的映像資源ID。隨後@drawable/必須是你已經儲存在你的項目的res/drawable/目錄下的位元影像映像的名稱。例如,“@drawable/ ic_action_search”指ic_action_search.png。同樣,title屬性將使用的在你的項目的res/values/目錄中的XML檔案中定義,在構建一個簡單的使用者介面討論字串資源。
Note: When creating icons and other bitmap images for your app, it‘s important that you provide multiple versions that are each optimized for a different screen density. This is discussed more in the lesson about Supporting Different Screens.
注意:當你的應用程式建立的表徵圖和其他位元影像映像,這一點很重要,你提供的每一個最佳化不同的螢幕密度的多個版本。這是更多的關於支援不同螢幕的課程進行討論。
If your app is using the Support Library for compatibility on versions as low as Android 2.1, the showAsAction attribute is not available from the android: namespace. Instead this attribute is provided by the Support Library and you must define your own XML namespace and use that namespace as the attribute prefix. (A custom XML namespace should be based on your app name, but it can be any name you want and is only accessible within the scope of the file in which you declare it.) For example:
如果您的應用程式使用的是支援庫的版本低到Android 2.1系統的相容性,showAsAction屬性從android: namespace是停用。相反,這個屬性是由支援庫提供的,你必須定義自己的XML命名空間,並使用該命名空間的屬性首碼。 (自訂XML命名空間,應根據你的應用程式的名字,但它可以是你想要的,只是檔案中聲明它的範圍之內訪問的任何名稱。)例如:
res/menu/main_activity_actions.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
yourapp:showAsAction="ifRoom" />
...
</menu>
Add the Actions to the Action Bar
給操作欄添加Action
--------------------------------------------------------------------------------
To place the menu items into the action bar, implement the onCreateOptionsMenu() callback method in your activity to inflate the menu resource into the given Menu object. For example:
若要將功能表項目到動作欄中,實現您的活動onCreateOptionsMenu()回調方法來設定菜單資源到給定的菜單對象。例如:
@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);
}
Respond to Action Buttons
響應Action按鈕
--------------------------------------------------------------------------------
When the user presses one of the action buttons or another item in the action overflow, the system calls your activity‘s onOptionsItemSelected() callback method. In your implementation of this method, call getItemId() on the given MenuItem to determine which item was pressed—the returned ID matches the value you declared in the corresponding <item> element‘s android:id attribute.
當使用者按下其中一個操作按鈕,或在Action溢出的另一個項,系統會調用您的活動的onOptionsItemSelected()回調方法。在實現此方法,調用給定功能表項目getItemId(),以確定哪些項目被按下,返回的ID,你在相應的<item>元素的聲明的價值相匹配Android:id屬性。
@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
添加向上Up按鈕的低層級Activity活動
--------------------------------------------------------------------------------
Figure 4. The Up button in Gmail.
圖4.在Gmail中的向上按鈕。
All screens in your app that are not the main entrance to your app (activities that are not the "home" screen) should offer the user a way to navigate to the logical parent screen in the app‘s hierarchy by pressing the Up button in the action bar.
在您的應用程式的所有螢幕,不是你的應用程式的主要入口(活動不屬於“Home首頁”畫面)應該為使用者提供了一種導航到應用程式的階層中的邏輯父螢幕,通過在操作欄按向上鍵。
When running on Android 4.1 (API level 16) or higher, or when using ActionBarActivity from the Support Library, performing Up navigation simply requires that you declare the parent activity in the manifest file and enable the Up button for the action bar.
在Android4.1(API等級16)或更高的運行時,或者使用從支援庫ActionBarActivity時,執行向上導航只是要求你申報的資訊清單檔中的父活動,使向上按鈕的操作欄。
For example, here‘s how you can declare an activity‘s parent in the manifest:
例如,這裡是你如何可以在manifest聲明Activity的父類:
<application ... >
...
<!-- The main/home activity (it has no parent activity) -->
<activity
android:name="com.example.myfirstapp.MainActivity" ...>
...
</activity>
<!-- A child of the main activity -->
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
Then enable the app icon as the Up button by calling setDisplayHomeAsUpEnabled():
然後通過調用setDisplayHomeAsUpEnabled()使應用程式圖示為向上Up按鈕:
@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);
}
Because the system now knows MainActivity is the parent activity for DisplayMessageActivity, when the user presses the Up button, the system navigates to the parent activity as appropriate—you do not need to handle the Up button‘s event.
因為系統現在知道MainActivity為DisplayMessageActivity父活動,當使用者按下向上鍵時,系統導航到父活動適當地-如果你不需要處理向上按鈕的事件。
For more information about up navigation, see Providing Up Navigation.
有關向上導航的更多資訊,請參閱提供向上導航。
本文翻譯自:https://developer.android.com/training/basics/actionbar/adding-buttons.html
Android官方入門文檔[6]添加Action按鈕