1. 程式人生 > >Android官方教程翻譯(6)——新增ActionBar

Android官方教程翻譯(6)——新增ActionBar

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.

ActionBar允許你去新增非常重要的action選單連線到應用的上下文,ActionBar中可以新增一個圖示或者文字的按鈕。Actions如果在ActionBar中放置不下則會隱藏。


Specify the Actions in XML

定義Actions在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.

Add an <item> element for each item you want to include in the action bar. For example:

所有的按鈕或者被隱藏的可被操作view都在選單資源XML檔案中定義,下面在你的工程的res/menu/directory下建立一個XML檔案,給actionBar新增action

新增方法如下:

<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>

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.)

如果ActionBar的顯示空間足夠大,查詢action就應該顯示並可見,但是設定action應該總是隱藏的(預設,所有的action都是隱藏的,但是最好是明確你的每個aciton的設計意圖)

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.

icon屬性需要指定一個圖片資源ID,在@drawable/路徑後的資源名必須是你已經在你工程res/drawable/目錄下存在的圖片名稱。例如,“@drawable/ic_action_search"對映到資源ic_action_search.png.同樣的,title屬性必須使用res/values/目錄下XML檔案中定義好的string字串,就如剛說的這樣,來建立一個簡單的介面。

If your app is using the Support Library for compatibility on versions as low as Android 2.1, the showAsActionattribute 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:

如果你用的類庫相容Android2.1以下版本,那麼Android名稱空間下的showAsAction屬性沒有用,為了代替庫檔案提供的這個屬性你必須定義你自己的XML名稱空間並使用這個名稱空間作為屬性的字首。(一個自定義的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

給ActionBar新增Action

To place the menu items into the action bar, implement the  callback method in your activity to inflate the menu resource into the given Menu object. For example:

將選單項新增到你的action bar,在你的activity中實現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  callback method. In your implementation of this method, call on the given  to determine which item was pressed—the returned ID matches the value you declared in the corresponding <item> element's android:id attribute.

當用戶按下一個action按鈕或者隱藏的其他選單,系統將自動回撥你的activity中的onOptionsItemSelected()方法。在你的方法實現中,呼叫getItemId()去得到相應的元素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

給低優先順序的Acitivity新增Up按鈕

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.

When running on Android 4.1 (API level 16) or higher, or when using from the Support Library, performing Upnavigation simply requires that you declare the parent activity in the manifest file and enable the Up button for the action bar.

For example, here's how you can declare an activity's parent in the manifest:

在你的應用中不是所有的介面都是主介面,通過按下ActionBar中的Up按鈕,應該提供給使用者一個父檢視。

當執行在Android4.1或者更高版本,或者新增高版本類庫,提供頂部導航需要在mainfest檔案中宣告父activity.

例如,在mainfest中如何宣告:

<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 :

然後通過呼叫setDisplayHomeAsUpEnable()是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 theUp button's event.