Setting Up the Action Bar

设置Action Bar

PREVIOUSNEXT

THIS LESSONTEACHES YOU TO

这节课教你

1.    Support Android 3.0 and Above Only仅仅支持3.0以上

2.    Support Android 2.1 and Above     支持2.1以上

YOU SHOULD ALSOREAD

·       Setting Up the Support Library

In its most basic form, the action bardisplays the title for the activity and the app icon on the left. Even in thissimple form, the action bar is useful for all activities to inform users aboutwhere they are and
to maintain a consistent identity for your app.

Action Bar最常见的形式是在左边显示标题和应用的图标。尽管很简单,但是很常见很有用,用他去显示用户登陆信息和身份。

Figure 1. An action bar with the app icon andactivity title.

Setting up a basic action bar requiresthat your app use an activity theme that enables the action bar. How to requestsuch a theme depends on which version of Android is the lowest supported byyour app. So this lesson
is divided into two sections depending on whichAndroid version is your lowest supported.

建立一个最基本的action bar需要一个可用主题的activity。怎样去获得这个主题依赖于Android程序的版本。所以这节课根据最低版本分为两个部分。

Support Android3.0 and Above Only


Beginning with Android 3.0 (API level11), the action bar is included in all activities that use the Theme.Holotheme
(or one of its descendants), whichis the default theme when either the targetSdkVersion orminSdkVersion attribute
is set to "11" or greater.

So to add the action bar to youractivities, simply set either attribute to 11 or
higher. For example:

Android3.0版本已经默认启用了Action Bar,因此只要版本高于“11”那么默认就会启动Action
Bar

例如下面配置。

<manifest ...
>

    <uses-sdk android:minSdkVersion="11" ...
/>

    ...

</manifest>

Note: If you've created a custom theme, besure it uses one of the Theme.Holo themes
as its parent. For details, see Styling theAction Bar.

Now the Theme.Holo theme
is applied to your app and allactivities show the action bar. That's it.

Support Android2.1 and Above


Adding the action bar when running on versionsolder than Android 3.0 (down to Android 2.1) requires that you include theAndroid Support Library in your application.

如果你是2.1以上3.0以下则需要下载支持的jar包。

To get started, read the Support LibrarySetup document
and set up the v7appcompat library (once you've downloaded the library package, follow theinstructions for Adding
librarieswith resources
).

Once you have the Support Libraryintegrated with your app project:

如果你的工程支持该库:

1.     Update your activity so that it extends ActionBarActivity.
For example:

让activity继承ActionBarActivity

public
class MainActivity
extends ActionBarActivity
{ ... }

2.     In your manifest file, update either the <application> element
or individual <activity> elements
to use one of the Theme.AppCompat themes.
For example:

在manifest文件中更改<application>元素或有一个<activity>元素使用Theme.AppCompat主题。例如:

<activity
android:theme="@style/Theme.AppCompat.Light" ...
>

Note: If you've created a custom theme, besure it uses one of the Theme.AppCompat themes
as its parent. For details, see Styling theAction Bar.

Now your activity includes the actionbar when running on Android 2.1 (API level 7) or higher

现在你的activity包含action bar当Android版本高于2.1

Remember to properly set your app's APIlevel support in the manifest:

记得去设置API级别支持action bar

<manifest ...
>

    <uses-sdk android:minSdkVersion="7"  android:targetSdkVersion="18"
/>

    ...

</manifest>

如果希望关闭ActionBar可以设置该应用的主题为,Xxx.NoActionBar

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.holo.NoAction" >
<activity
android:name="com.example.test.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

一旦关闭ActionBar该不能使用

实际的项目中,通常推荐使用代码来控制ActionBar的显示和影藏,ActionBar提供了如下方法:

show():显示

hide()影藏

package com.example.test;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.View; public class MainActivity extends Activity {
ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取该Activity的ActionBar
//只有当应用主题没有关闭ActionBar时,该代码才能返回ActionBar
actionBar = getActionBar();
} //为“显示ActionBar”按钮定义事件处理方法
public void showActionBar(View source){
//显示
actionBar.show();
} //为“隐藏ActionBar"按钮定义事件处理方法
public void hideActionBar(View source){
//隐藏
actionBar.hide();
}
}

Android官方教程翻译(5)——设置ActionBar的更多相关文章

  1. Android官方教程翻译(6)——添加ActionBar

    The action bar allows you to add buttons for the most important action items relating to the app's c ...

  2. Android官方教程翻译(4)——启动另一个Activity

    Starting Another Activity 启动另一个Activity PREVIOUSNEXT THIS LESSON TEACHES YOU TO 这节课教你 1.   Respond t ...

  3. Android官方教程翻译(1)——创建第一个Android应用

    转载请注明出处:http://blog.csdn.net/dawanganban/article/details/9822431 Building Your First App GETSTARTED ...

  4. Android官方教程翻译(3)——创建一个简单的用户界面

    转载请注明出处:http://blog.csdn.net/dawanganban/article/details/9839523 Building a Simple User Interface 创建 ...

  5. Android官方教程翻译(2)——运行第一个程序

    转载请注明出处:http://blog.csdn.net/dawanganban/article/details/9823623 Running Your App PREVIOUSNEXT THIS ...

  6. c# MongoDB Driver 官方教程翻译

    先贴官方文档地址:http://mongodb.github.io/mongo-csharp-driver/2.5/getting_started/quick_tour/ 安装部分很简单,nuget搜 ...

  7. Pytest权威教程(官方教程翻译)

    Pytest权威教程01-安装及入门 Pytest权威教程02-Pytest 使用及调用方法 Pytest权威教程03-原有TestSuite的执行方法 Pytest权威教程04-断言的编写和报告 P ...

  8. android 官方教程地址和一个中文教程

    https://developer.android.google.cn/guide/components/fundamentals http://www.runoob.com/w3cnote/andr ...

  9. android 官方教程中文版

    感谢这些默默奉献的人 :) https://github.com/kesenhoo/android-training-course-in-chinese http://hukai.me/android ...

随机推荐

  1. 【BZOJ 3172】单词

    [链接]h在这里写链接 [题意]     给你n个单词;     这n个单词组成了一篇文章;     问你每个单词在这篇文章中出现了多少次.     其中每个单词之间用一个逗号隔开->组成一篇文 ...

  2. HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)

    HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意:  给一个序列由 ...

  3. 自定义 matplotlib 设置

    Customizing plots with style sheets import matplotlib as mpl 查看配置文件所在的目录:mpl.get_configdir() 1. 自定义 ...

  4. HDU 1800 Flying to the Mars Trie或者hash

    http://acm.hdu.edu.cn/showproblem.php?pid=1800 题目大意: 又是废话连篇 给你一些由数字组成的字符串,判断去掉前导0后那个字符串出现频率最高. 一开始敲h ...

  5. [RxJS] Multicast with a selector argument, as a sandbox

    Let's explore a different use of the multicast() operator in RxJS, where you can provide a selector ...

  6. C语言之基本算法11—牛顿迭代法求平方根

    //迭代法 /* ================================================================== 题目:牛顿迭代法求a的平方根!迭代公式:Xn+1 ...

  7. Oracle 12C R2 on Linux 7.X 单实例静默安装文档

    禁用防火墙systemctl stop firewalld.servicesystemctl disable firewalld.service 禁用SELinuxcat /etc/selinux/c ...

  8. bootstrap如何把表单select input button弄在一行

    bootstrap很多折叠样式css都已经写好,可以直接用,很方便.但是,如果遇到一些bootstrap文档里面没有的例子,估计很多初学者都懵了,然后会折腾很久也未见得有效.今天主要讲如何把selec ...

  9. Android自己定义控件2-简单的写字板控件

    概述 上一篇文章我们对自己定义控件进行了一个大体的知识介绍. 今天就来学习自己定义一个简单的写字板控件. 先来看看效果图 就是简单的依据手指写下的轨迹去画出内容 实现 在上一篇文章里提到了androi ...

  10. Codeforces Round #315 (Div. 2)——C. Primes or Palindromes?

    这道题居然是一个大暴力... 题意: π(n):小于等于n的数中素数的个数 rub(n) :小于等于n的数中属于回文数的个数 然后给你两个数p,q,当中A=p/q. 然后要你找到对于给定的A.找到使得 ...