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以上
·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 theTheme.Holotheme
(or one of its descendants), whichis the default theme when either thetargetSdkVersionorminSdkVersionattribute
is set to"11"or greater.
So to add the action bar to youractivities, simply set either attribute to11or
higher. For example:
Android3.0版本已经默认启用了Action Bar,因此只要版本高于“11”那么默认就会启动Action
Bar
例如下面配置。
<manifest ...
>
<uses-sdk android:minSdkVersion="11" ...
/>
...
</manifest>
Now theTheme.Holotheme
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 theSupport LibrarySetupdocument
and set up thev7appcompatlibrary (once you've downloaded the library package, follow theinstructions forAdding
librarieswith resources).
Once you have the Support Libraryintegrated with your app project:
如果你的工程支持该库:
1. Update your activity so that it extendsActionBarActivity.
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 theTheme.AppCompatthemes.
For example:
在manifest文件中更改<application>元素或有一个<activity>元素使用Theme.AppCompat主题。例如:
<activity
android:theme="@style/Theme.AppCompat.Light" ...
>
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();
}
}
分享到:
相关推荐
① 作为“action选项”,在ActionBar上直接显示“选项菜单”(OptionsMenu)—— 为用户进行某项操作提供直接的访问; 作为“action项”没有出现在ActionBar上得菜单项会被置于“更多...”菜单项 中,在...
ActionBarDemo ActionBar初探——ActionBar简述及三类ActionBar的使用 详细内容请看: http://blog.csdn.net/hander_wei/article/details/24182019
android-controls-演示 Android 控件示例 - Drawer、ActionBar、ViewPager 等。
Toolbar与ActionBar最大的区别就是Toolbar更加自由、可控。这也是Google在逐渐使用Toolbar替换ActionBar的原因,要使用Toolbar必须引入appcompat-v7支持,并设置主题为NoActionBar。
这些都是该作者在学习Android开发过程中觉得比较好的教程和博客 如题 如题 android自定义view讲解 给高级 Android 工程师的进阶手册 高质量Android社区 —— 国外著名的问答社区 —— 长期更新最新前言资讯 —— ...
Android UI开发第二十四篇——Action Bar》。ActionBar的API被添加在Android3.0(API 级别 11)中,低版本的还是用不了,根本不能适配支持Android 2.X系列的应用。很幸运有第三方开源的actionbarsherlock支持使得...
——————————————————————————– 效果预览 自定义Menu后不影响原生MD的任何效果。可以通过外部来控制显示的文字和颜色等属性。 需求描述与分析 通常我们会自定义Titlebar,可以实现很多...
包括ListView、ActionBar、Menu、ViewPager、Gallery、GridView、ImageView、ProgressBar、TextView、ScrollView、TimeView、TipView、FlipView、ColorPickView、GraphView、UI Style、其他 Android 开源项目第二篇...
包括ListView、ActionBar、Menu、ViewPager、Gallery、GridView、ImageView、ProgressBar、TextView、ScrollView、TimeView、TipView、FlipView、ColorPickView、GraphView、UI Style、其他 Android 开源项目第二篇...
Android UI开发第二十五篇——分享一篇自定义的 Action Bar
如下图所示,到这里 Activity 持有一个 PhoneWindow 对象,PhoneWindow 中有一个 DecorView ,DecorView 中加载了一个基础的资源布局,里面有 title,ActionBar 等,肯定存在一个 id 为 R.id.ccontent 的资源布局。...
Android的UI并不算美观,以至于很多IT公司在进行界面设计的时候,为了保证双平台的统一性,强烈要求Android端的界面风格必须与iOS端一致,我认为这里非常不合理的,同一操作系统中各个应用之间的界面统一性要远比一...