今天我们继续来讲解ActionBar的使用,不清楚这个类的读者可以翻阅博主前几篇的文章或者在网络上查阅相关
的资料,关于这个类讲解的文章还是很多的,功能确实也很强大。好的,话不多说,让我们赶快进入正题吧。
一、使用应用图标实现层级导航
在默认的情况下,应用程序图标显示在操作栏的左边。你能够把这个图标当做操作项来使用,应用程序可以在这
个图标上响应以下两个操作其中之一:
<1> 返回应用程序的“主”Activity;
<2> 向应用程序上级页面导航。
要实现应用程序图标能够向上导航,首先就要在你的ActionBar中调用SetDisplayHomeAsUpEnabledtrue(true)方法。
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- ActionBar actionBar = getActionBar();
- actionBar.setDisplayHomeAsUpEnabled(true);
- ...
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case android.R.id.home:
- Intent intent = new Intent(this, HomeActivity.class);
- intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- startActivity(intent);
- return true;
- default:
- return super.onOptionsItemSelected(item);
- }
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case android.R.id.home:
- // This is called when the Home (Up) button is pressed
- // in the Action Bar.
- Intent parentActivityIntent = new Intent(this, MyParentActivity.class);
- parentActivityIntent.addFlags(
- Intent.FLAG_ACTIVITY_CLEAR_TOP |
- Intent.FLAG_ACTIVITY_NEW_TASK);
- startActivity(parentActivityIntent);
- finish();
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
当在应用中使用fragments时,单一的FragmentTransaction对象能够表示环境的变化,应该被添加到back堆栈。例如,如果你要实现一个master/detail流程,通过换出fragments,你应该确保在detail界面上点击Back按钮回退到master界面。
- getFragmentManager().beginTransaction().add(detailFragment, "detail")
- // Add this transaction to the back stack and commit.
- .addToBackStack()
- .commit();
- getFragmentManager().addOnBackStackChangedListener(
- new FragmentManager.OnBackStackChangedListener() {
- public void onBackStackChanged() {
- // Update your UI here.
- }
- });
- Intent externalActivityIntent = new Intent(Intent.ACTION_PICK);
- externalActivityIntent.setType("image/*");
- externalActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
- startActivity(externalActivityIntent);
![20130608225935968.jpg 20130608225935968.jpg](http://www.apkbus.com/data/attachment/forum/201307/11/160944kkfw7p575ckeaezk.jpg)
![20130608230030875.jpg 20130608230030875.jpg](http://www.apkbus.com/data/attachment/forum/201307/11/160945zr6gdgjtovd7ttl6.jpg)
![20130608230135171.jpg 20130608230135171.jpg](http://www.apkbus.com/data/attachment/forum/201307/11/160946n94jxqqd24z224jg.jpg)
![20130608230140234.jpg 20130608230140234.jpg](http://www.apkbus.com/data/attachment/forum/201307/11/160947vh5r38x3hocm35r8.jpg)
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <Button
- android:id="@+id/other_btn"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="160dp"
- android:text="Other_Activity" />
- <Button
- android:id="@+id/external_btn"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_below="@+id/other_btn"
- android:text="External_Activity" />
- </RelativeLayout>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- </RelativeLayout>
- package com.yangyu.myactionbar04;
- import android.app.ActionBar;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- public class MainActivity extends Activity {
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- initView();
- }
- /**
- * 初始化组件
- */
- private void initView(){
- final ActionBar actionBar = getActionBar();
-
- actionBar.setHomeButtonEnabled(false);
-
- this.findViewById(R.id.other_btn).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent intent = new Intent(MainActivity.this, OtherActivity.class);
- startActivity(intent);
- }
- });
-
- this.findViewById(R.id.external_btn).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- //调用图片浏览器
- Intent externalActivityIntent = new Intent(Intent.ACTION_PICK);
- externalActivityIntent.setType("image/*");
- externalActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
- startActivity(externalActivityIntent);
- }
- });
- }
- }
- package com.yangyu.myactionbar04;
- import android.app.ActionBar;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.v4.app.FragmentActivity;
- import android.support.v4.app.NavUtils;
- import android.support.v4.app.TaskStackBuilder;
- import android.view.MenuItem;
- public class OtherActivity extends FragmentActivity {
-
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_other);
-
- final ActionBar actionBar = getActionBar();
-
- actionBar.setDisplayHomeAsUpEnabled(true);
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case android.R.id.home:
- Intent upIntent = new Intent(this, MainActivity.class);
- if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
- TaskStackBuilder.from(this)
- //如果这里有很多原始的Activity,它们应该被添加在这里
- .addNextIntent(upIntent)
- .startActivities();
- finish();
- } else {
- NavUtils.navigateUpTo(this, upIntent);
- }
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
![](http://www.apkbus.com/static/image/filetype/zip.gif)