ActionBar(官方文档学习记录三)
2015-12-09 00:00
337 查看
Adding an Up Action
PreviousNextThis lesson teaches you to
Declare a Parent ActivityEnable the Up Button
See Also
Providing Up NavigationYour app should make it easy for users to find their way back to the app's main screen. One simple way to do this is to provide an Up button on the app bar for all activities except the main one. When the user selects the Up button, the app navigates to the parent activity.
This lesson shows you how to add an Up button to an activity by declaring the activity's parent in the manifest, and enabling the app bar's Up button.
Declare a Parent Activity
To support the up functionality in an activity, you need to declare the activity's parent. You can do this in the app manifest, by setting anandroid:parentActivityNameattribute.
The
android:parentActivityNameattribute was introduced in Android 4.1 (API level 16). To support devices with older versions of Android, define a
<meta-data>name-value pair, where the name is
"android.support.PARENT_ACTIVITY"and the value is the name of the parent activity.
For example, suppose your app has a main activity named
MainActivityand a single child activity. The following manifest code declares both activities, and specifies the parent/child relationship:
<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.MyChildActivity" android:label="@string/title_activity_child" 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>
Enable the Up Button
To enable the Up button for an activity that has a parent activity, call the app bar'ssetDisplayHomeAsUpEnabled()method. Typically, you would do this when the activity is created. For example, the following
onCreate()method sets a
Toolbaras the app bar for
MyC 3ff8 hildActivity, then enables that app bar's Up button:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_child); // my_child_toolbar is defined in the layout file Toolbar myChildToolbar = (Toolbar) findViewById(R.id.my_child_toolbar); setSupportActionBar(myChildToolbar); // Get a support ActionBar corresponding to this toolbar ActionBar ab = getSupportActionBar(); // Enable the Up button ab.setDisplayHomeAsUpEnabled(true); }
You do not need to catch the up action in the activity's
onOptionsItemSelected()method. Instead, that method should call its superclass, as shown in Respond to Actions. The superclass method responds to the Up selection by navigating to the parent activity, as specified in the app manifest.
相关文章推荐
- XMPP协议,全新定义基于Tiagse的社交聊天系统
- 线程安全性问题---->同步代码块
- eclipse之debug找不到源
- 活用云盒子工作间的几个小技巧
- 在HBase中使用Hadoop本地库(适用于HBase 1.x及Hadoop 2.x)
- IOS研发之路-Xcode7.1真机调试出现"Could not find Developer
- 各个语言的基本数据类型:
- 如何用不干胶打印流水号
- VC 重新启动程序
- angularjs实现url权限控制
- linux awk命令详解
- php扩展ImageMagick图片库处理扩展
- 在Ruby on Rails项目中用tesseract ocr读取图片上文字
- ios系统中各种设置项的url链接 ,实现跳转系统页面设置
- UITextField设置大全
- 我买了一辆自行车
- 《IT蓝豹》一个不错的loading效果
- Java中字节与十六进制的转换
- 归档解档的内部实现机理
- mutating的使用(swift2.0)