1:ActionBar

来源:互联网 发布:python excel数据处理 编辑:程序博客网 时间:2024/05/21 22:46

一、核心功能:

1、一个专门的空间用来显示你的app标识,并且还可以指出目前所处在app哪个页面。

2、以一种可预见的方式访问重要的操作(比如检索)

3、支持导航和视图切换(通过Tabs和下拉列表)

二、建立ActionBar

设置一个基本的ActionBar,需要你的app使用一个action bar可用的Activity主题。如何声明主题又取决于你的app支持的Android最低版本。

a.仅支持Android3.0以上版本

从Android3.0开始,所有使用Theme.Holo主题(或者它的子类)的activity都包含了action bar,当targetSdkVersion或者minSdkVersion属性被设置成“11”或更大时,它是默认主题。如果创建自定义主题,继承Theme.Holo的一个主题作为父类即可。

b.支持Android2.1及以上版本

需加载Android Support库(引入v7 appcompat library),然后让Activity继承ActionBarActivity,最后在清单文件中给<application>或者<activity>使用一个Theme.AppCompat主题。如:

<activity android:theme="@style/Theme.AppCompat.Light" ... >
自定义主题,同上继承Theme.AppCompat。

三、添加ActionBar

直接出现在action bar中的icon或文本被称作action buttons(操作按钮),安排不下的或不足够重要的操作被隐藏在action overflow中。

a.xml中定义布局样式

res/menu下定义布局

b.添加响应事件

先在activity中实现onCreateOptionsMenu()中inflate菜单资源:

@Overridepublic boolean onCreateOptionsMenu(Menu menu) {<span style="white-space:pre"></span>// 为ActionBar扩展菜单项<span style="white-space:pre"></span>MenuInflater inflater = getMenuInflater();<span style="white-space:pre"></span>inflater.inflate(R.menu.main_activity_actions, menu);<span style="white-space:pre"></span>return super.onCreateOptionsMenu(menu);}
然后添加响应事件:在onOptionsItemSelected()

@Overridepublic boolean onOptionsItemSelected(MenuItem item) {<span style="white-space:pre"></span>// 处理动作按钮的点击事件<span style="white-space:pre"></span>switch (item.getItemId()) {<span style="white-space:pre"></span>case R.id.action_search:<span style="white-space:pre"></span>openSearch();<span style="white-space:pre"></span>return true;<span style="white-space:pre"></span>case R.id.action_settings:<span style="white-space:pre"></span>openSettings();<span style="white-space:pre"></span>return true;<span style="white-space:pre"></span>default:<span style="white-space:pre"></span>return super.onOptionsItemSelected(item);<span style="white-space:pre"></span>}}
c.为Activity添加返回到前一个Activity按钮
在action bar中为用户提供一个导航到逻辑父屏的up button。在AndroidMainfest文件中的action标签中添加:

android:parentActivityName="逻辑父Activity"
然后在onCreate()中把app icon设置成可用的向上按钮:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);// 如果你的minSdkVersion属性是11活更高, 应该这么用:// getActionBar().setDisplayHomeAsUpEnabled(true);

四、ActionBar的风格变化

a.先找到系统的主题

Android包含两个基本的activity主题,这两个主题决定了action bar的颜色:

Theme.Holo:一个“暗”的主题

Theme.Holo.Light:一个“淡”的主题

当使用Support库时,必须使用Theme.AppCompat主题替代:

Theme.AppCompat和Theme.AppCompat.Light

b.自定义主题
1)自定义背景

重写主题,然后主题中引入自定义的action bar样式:重写actionBarStyle属性来改变action bar的背景。actionBarStyle属性指向另一个样式;在该样式里,通过指定一个drawable资源来重写background属性。

<?xml version="1.0" encoding="utf-8"?><resources><!-- 应用于程序或者活动的主题 --><span style="white-space:pre"></span><style name="CustomActionBarTheme"<span style="white-space:pre"></span>parent="@android:style/Theme.Holo.Light.DarkActionBar"><span style="white-space:pre"></span><item name="android:actionBarStyle">@style/MyActionBar</item><span style="white-space:pre"></span></style><!-- ActionBar 样式 --><span style="white-space:pre"></span><style name="MyActionBar"<span style="white-space:pre"></span>parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse"><span style="white-space:pre"></span><item name="android:background">@drawable/actionbar_background</item><span style="white-space:pre"></span>自定义背景<span style="white-space:pre"></span>仅支持 Android 3.0 和更高<span style="white-space:pre"></span></style></resources>
最后在AndroidMainfest中使用该主题。

关于支持2.1请自行百度,不再说明。

2)自定义文本颜色

需要分别重写每个元素的属性:

Action Bar的标题:创建自定义样式,并指定textColor属性;同时,在你的自定义actionBarStyle中为titleTextStyle属性指定为刚才的自定义样式。

Action bar tabs:在你的activity主题中重写actionBarTabTextStyle。

Action按钮:在你的activity主题中重写actionMenuTextColor。

仅支持Android3.0和更高,关于2.1自行百度:

<?xml version="1.0" encoding="utf-8"?><resources><span style="white-space:pre"></span><!-- 应用于程序或者活动的主题 --><span style="white-space:pre"></span>支持 Android 2.1 和更高<span style="white-space:pre"></span>自定义文本颜色<span style="white-space:pre"></span>仅支持 Android 3.0 和更高<span style="white-space:pre"></span><style name="CustomActionBarTheme"<span style="white-space:pre"></span>parent="@style/Theme.Holo"><span style="white-space:pre"></span><item name="android:actionBarStyle">@style/MyActionBar</item><span style="white-space:pre"></span><item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item><span style="white-space:pre"></span><item name="android:actionMenuTextColor">@color/actionbar_text</item><span style="white-space:pre"></span></style><span style="white-space:pre"></span><!-- ActionBar 样式 --><span style="white-space:pre"></span><style name="MyActionBar"<span style="white-space:pre"></span>parent="@style/Widget.Holo.ActionBar"><span style="white-space:pre"></span><item name="android:titleTextStyle">@style/MyActionBarTitleText</item><span style="white-space:pre"></span></style><span style="white-space:pre"></span><!-- ActionBar 标题文本 --><span style="white-space:pre"></span><style name="MyActionBarTitleText"<span style="white-space:pre"></span>parent="@style/TextAppearance.Holo.Widget.ActionBar.Title"><span style="white-space:pre"></span><item name="android:textColor">@color/actionbar_text</item><span style="white-space:pre"></span></style><span style="white-space:pre"></span><!-- ActionBar Tab标签 文本样式 --><span style="white-space:pre"></span><style name="MyActionBarTabText"<span style="white-space:pre"></span>parent="@style/Widget.Holo.ActionBar.TabText"><span style="white-space:pre"></span><item name="android:textColor">@color/actionbar_text</item><span style="white-space:pre"></span></style></resources>

3)自定义Tab Indicator

重写actionBarTabStyle属性来改变navigation tabs使用的指示器。actionBarTabStyle属性指向另一个样式资源;在该样式资源里,通过指定一个state-list drawable来重写background属性。

<?xml version="1.0" encoding="utf-8"?><span style="white-space:pre"></span>仅支持 Android 3.0 和更高<span style="white-space:pre"></span><resources><span style="white-space:pre"></span><!-- 应用于程序或活动的主题 --><span style="white-space:pre"></span><style name="CustomActionBarTheme"<span style="white-space:pre"></span>parent="@style/Theme.Holo"><span style="white-space:pre"></span>item name="android:actionBarTabStyle">@style/MyActionBarTabs</item><span style="white-space:pre"></span></style><span style="white-space:pre"></span><!-- ActionBar tabs 标签样式 --><span style="white-space:pre"></span><style name="MyActionBarTabs"<span style="white-space:pre"></span>parent="@style/Widget.Holo.ActionBar.TabView"><span style="white-space:pre"></span><!-- 标签指示器 --><span style="white-space:pre"></span><item name="android:background">@drawable/actionbar_tab_indicator</item><span style="white-space:pre"></span></style></resources>

五、ActionBar覆盖叠加

hide()和show()可显示和隐藏action bar。但是,这将导致activity基于新尺寸重新计算和绘制布局。为了避免这种情况,可为action  bar启用叠加模式。在叠加模式下,所有可用的空间都会被用来布局就像ActionBar不存在一样,并且action bar会叠加在你的布局之上。这样布局顶部就会有点被遮挡,但当action bar隐藏或显示时,系统不需要调整布局。

Note:如果你希望action bar下面的布局部分可见,可创建一个背景部分透明的自定义样式的action bar。

a.启用叠加模式

需要自定义一个主题,该主题继承于已经存在的action bar主题,并设置android:windowActionBarOverlay属性为true。2.1自行百度。

叠加模式可能会遮挡一部分该显示的内容,为了避免这种情况,可用padding或margin来实现:android:paddingTop="?android:attr/actionBarSize"

0 0
原创粉丝点击