【Android】为需要支持API 11之前的Activity添加Action Bar的一种解决方案
首先汗一个,题目打出来我就觉得像是在写论文…… 家里生了个娃,好久没有写东西了……
做Android开发有一个很头疼的地方就是随着sdk的演进,很多新东西被加进来。但由于这样那样的限制, 不是所有的新玩意儿都会被加入support包里面。如果你的team里面恰好有一个矫情的PM或者designer的话,很可能发生的一件事情就是要你把一些fashion的新东西加进一个需要支持到api 11之前的项目中去。看在api 11之前的设备的活跃率大概还有20%左右的份上,作为一个敬业的dev就需要绞尽脑汁来实现这一合理的需求。
API 11开始加入的新玩意儿除了fragment就是action bar了, 这东西确实挺有用的,尤其是在一些没有按键设计的手机上, 比如MX3, Mate。如果不想激活屏幕下端的按键栏,有action bar的home button来做back的动作确实方便。
废话说了一堆,进入正题。
虽然google在support v7中提供了action bar的支持, 但那个解决方案真是太重了。 接下来说到得解法一句话就能说明白:拿掉title bar换上自己化的一个假的action bar。
首先我们要准备一个action bar的layout, 里面包含最基本的元素:home button, division image, title
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/actionbar_widget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@color/actionbar_bg_color"
android:orientation="horizontal" >
<Button
android:id="@+id/actionbar_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@drawable/actionbar_home_btn"
android:padding="0dip"
android:paddingRight="4dip"
android:visibility="visible" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/actionbar_division"
android:contentDescription="@null" />
<TextView
android:id="@+id/actionbar_title"
android:layout_marginLeft="4dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
android:text="@null"
android:textColor="@color/actionbar_text_color"
android:textSize="22sp"
android:singleLine="true" />
</LinearLayout>
使用的时候将这个layout include到你的activity layout的最前面:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@id/activity_frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent"android:orientation="vertical" >
<include
android:id="@id/actionbar"
android:layout_width="fill_parent"
layout="@layout/actionbar" />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
然后在style.xml或者theme.xml里面加一个自定义的theme:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="ActionBarTheme" parent="android:Theme.Light.NoTitleBar">
</style>
</resources>
无论继承那个theme,只要是NoTitleBar的即可。
在AndroidManifest.xml里面讲这个theme设置给activity
<activity
android:name=".Activity"
android:theme="@style/ActionBarTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
然后就可以在activity layout文件的Graphic layout页面看到效果了。
按键响应和title设置略去不说了。
另外, 如果要想为preference activity添加action bar,只需按照上面的layout写。但在onCreate()里面一定要先设置preference界面的layout再设置activity的layout, 然后再设置action bar。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); addPreferencesFromResource(R.layout.settings);
setContentView(R.layout.activity);
setupActionBar();
}
这样preference的layout就会被嵌入到id为@android:id/list的list view里面。因为preference activity是继承自List activity, 而List activity的layout是这样写的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp"> <ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/> <TextView android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>
它们插入list item时都用 com.android.internal.R.id.list 这个ID来找list view。所以这是只是一个精彩的“移形换位”, 完全安全可靠。
《END》
【Android】为需要支持API 11之前的Activity添加Action Bar的一种解决方案的更多相关文章
- 第三部分:Android 应用程序接口指南---第二节:UI---第四章 Action Bar
第4章 Action Bar Action Bar是一个能用于确定应用程序和用户的位置,并提供给用户操作和导航模式的窗口功能.如果需要显著地展示当前用户的操作或导航,应该使用Action Bar,因为 ...
- 【Android Developers Training】 5. 序言:添加Action Bar
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- Android UI开发第三十五篇——AppCompat实现Action Bar
每一位Android开发者对Action Bar这种设计都不陌生了,毕竟它已经发布了至少两年了.Android团队发布Action Bar设计规范时同时放出了ActionBar的Api来支持这种设计. ...
- 【Android Developers Training】 9. 覆盖于布局之上的Action Bar
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- Android界面编程--使用活动条(ActionBar)--添加Action View
ActionBar除了显示Action Item 外,还能显示普通的ui组件 2种方式添加Action View 1.指定ActionView的实现类 2.指定ActionView对应的视图资源 实现 ...
- 【Android Developers Training】 6. 配置Action Bar
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- Android设计和开发系列第二篇:Action Bar(Develop—API Guides)
Action Bar IN THIS DOCUMENT Adding the Action Bar Removing the action bar Using a logo instead of an ...
- Android Action Bar简介
Android Action Bar简介 Design: Action Bar Action Bar是在屏幕顶端的一部分内容,通常在整个app进行中都保持存在. 它提供了几个关键的功能: 1.使得重要 ...
- Android 自定义title 之Action Bar
Android 自定义title 之Action Bar 2014-06-29 飞鹰飞龙... 摘自 博客园 阅 10519 转 25 转藏到我的图书馆 微信分享: Action Ba ...
随机推荐
- rsync同步命令详解
一.rsync命令的解释sync(Synchronize,即“同步”)为UNIX操作系统的标准系统调用,功能为将内核文件系统缓冲区的所有数据(也即预定将通过低级I/O系统调用写入存储介质的数据)写入存 ...
- Solidity的三种合约间的调用方式 call、delegatecall 和 callcode
0x00 前言 Solidity(http://solidity.readthedocs.io/en/v0.4.24/) 是一种用与编写以太坊智能合约的高级语言,语法类似于 JavaScript. S ...
- 4412 uboot上手
1,了解 print 查看UBOOT软件的环境变量 (变量名=变量) setenv.saveenv setenv abc 100 200 设置 添加一个变量值 修改一个已有的变量 ...
- 自定义Xadmin
1.启动Xadmin INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.content ...
- 【Spark2.0源码学习】-7.Driver与DriverRunner
承接上一节内容,Client向Master发起RequestSubmitDriver请求,Master将DriverInfo添加待调度列表中(waitingDrivers),下面针对于Dri ...
- [SoapUI] 如何同时调用Global Script Library(放在SoapUI安装目录)和项目特有的Script Libary(放在项目代码下)
SoapUI 支持引入多个package: Global Script library : 在SoapUI工具File->Preference中设置Project Script Library: ...
- AutoCAD开发5--批量修改dwg文件
Dim files, path, filename path = ThisDrawing.Utility.GetString(True, "输入dwg文件所在路径:") 'dwg文 ...
- 连接hive
bin/hiveserver2 nohup bin/hiveserver2 1>/var/log/hiveserver.log 2>/var/log/hiveserver.err & ...
- HashMap TreeMap的区别
Map主要用于存储健值对,根据键得到值,因此不允许键重复(重复就覆盖了),但允许值重复.Hashmap 是一个最常用的Map,它根据键的HashCode 值存储数据,根据键可以直接获取它的值,具有很快 ...
- 用installshield2013 将winform程序打包成exe执行程序
前期准备工作 1,一个已经测试通过的winform程序 2,安装好的installshield2013插件 ps:一般VS都没有安装此插件,需要自己去下载 打包步骤 1,新建一个打包程序 ps:如 ...