Android官方入门文档[6]添加Action按钮

简介: Android官方入门文档[6]添加Action按钮 Adding Action Buttons添加Action按钮 This lesson teaches you to1.

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

目录
相关文章
|
22天前
|
XML Java Android开发
Android每点击一次按钮就添加一条数据
Android每点击一次按钮就添加一条数据
23 1
|
1月前
|
测试技术 API 调度
【Android 从入门到出门】第七章:开始使用WorkManager
【Android 从入门到出门】第七章:开始使用WorkManager
20 3
【Android 从入门到出门】第七章:开始使用WorkManager
|
1月前
|
存储 Android开发 C++
【Android 从入门到出门】第五章:使用DataStore存储数据和测试
【Android 从入门到出门】第五章:使用DataStore存储数据和测试
31 3
|
1月前
|
Android开发
【Android 从入门到出门】第四章:现代Android开发中的导航
【Android 从入门到出门】第四章:现代Android开发中的导航
22 2
【Android 从入门到出门】第四章:现代Android开发中的导航
|
1月前
|
XML API Android开发
【Android 从入门到出门】第三章:使用Hilt处理Jetpack Compose UI状态
【Android 从入门到出门】第三章:使用Hilt处理Jetpack Compose UI状态
26 4
|
1月前
|
存储 XML 编译器
【Android 从入门到出门】第二章:使用声明式UI创建屏幕并探索组合原则
【Android 从入门到出门】第二章:使用声明式UI创建屏幕并探索组合原则
48 3
|
1月前
|
存储 SQL 数据库
【Android 从入门到出门】第六章:使用Room数据库并测试
【Android 从入门到出门】第六章:使用Room数据库并测试
29 4
|
1月前
|
存储 Android开发
【Android 从入门到出门】第八章:分页入门指南
【Android 从入门到出门】第八章:分页入门指南
21 3
|
1月前
|
IDE Java 开发工具
【Android 从入门到出门】第一章:Android开发技能入门指南
【Android 从入门到出门】第一章:Android开发技能入门指南
62 3
|
2月前
|
JSON Java Go