首先汗一个,题目打出来我就觉得像是在写论文…… 家里生了个娃,好久没有写东西了……

做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的一种解决方案的更多相关文章

  1. 第三部分:Android 应用程序接口指南---第二节:UI---第四章 Action Bar

    第4章 Action Bar Action Bar是一个能用于确定应用程序和用户的位置,并提供给用户操作和导航模式的窗口功能.如果需要显著地展示当前用户的操作或导航,应该使用Action Bar,因为 ...

  2. 【Android Developers Training】 5. 序言:添加Action Bar

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  3. Android UI开发第三十五篇——AppCompat实现Action Bar

    每一位Android开发者对Action Bar这种设计都不陌生了,毕竟它已经发布了至少两年了.Android团队发布Action Bar设计规范时同时放出了ActionBar的Api来支持这种设计. ...

  4. 【Android Developers Training】 9. 覆盖于布局之上的Action Bar

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  5. Android界面编程--使用活动条(ActionBar)--添加Action View

    ActionBar除了显示Action Item 外,还能显示普通的ui组件 2种方式添加Action View 1.指定ActionView的实现类 2.指定ActionView对应的视图资源 实现 ...

  6. 【Android Developers Training】 6. 配置Action Bar

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  7. 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 ...

  8. Android Action Bar简介

    Android Action Bar简介 Design: Action Bar Action Bar是在屏幕顶端的一部分内容,通常在整个app进行中都保持存在. 它提供了几个关键的功能: 1.使得重要 ...

  9. Android 自定义title 之Action Bar

    Android 自定义title 之Action Bar 2014-06-29  飞鹰飞龙...  摘自 博客园  阅 10519  转 25 转藏到我的图书馆   微信分享:   Action Ba ...

随机推荐

  1. 集群环境下定时调度的解决方案之Quartz集群

    集群环境可能出现的问题 在上一篇博客我们介绍了如何在自己的项目中从无到有的添加了Quartz定时调度引擎,其实就是一个Quartz 和Spring的整合过程,很容易实现,但是我们现在企业中项目通常都是 ...

  2. JavaSE基础知识(5)—面向对象(5.1类和对象概念、创建及内存分配)

    一.类和对象的相关概念 1.面向对象和面向过程的理解 面向对象和面向过程都属于解决问题的思考方式.面向过程:以执行者的角度思考问题,侧重于“怎么做”,比较适合解决小型项目面向对象:以指挥者的角度思考问 ...

  3. css杂烩

    持续更新... 在css中处理外边距合并时,思路是触发BFC,因为在创建了块级格式化的元素中(垂直排列),不会和它的子元素发生外边距合并.而BFC可以通过float(不包括none),overflow ...

  4. Fefora 14 源

    默认的源不能用,需要用下边的源路径. [fedora] name=Fedora $releasever - $basearch failovermethod=priority #baseurl=htt ...

  5. Android工具

    2018-09-27 安卓签名工具 AndroidKiller 比如包打好了,想替换一个图片,就用zip打开,替换图片,重新签名.

  6. mysql 在update中实现子查询的方式

    当使用mysql条件更新时--最先让人想到的写法 UPDATE buyer SET is_seller=1 WHERE uid IN (SELECT uid FROM seller) 此语句是错误的, ...

  7. Python11/23--mysql用户管理/pymysql

    1.mysql用户管理 定义:数据安全是很重要的,不能随便分配root账户,应该按照不同开发岗位分配不同的账户和权限 mysql中将用户相关的数据放在mysql库中 user→db→tables_pr ...

  8. python_day1_变量

    一.变量 定义: 通俗来讲可变化的量称之为变量,专业的解释为:把程序运算的中间结果临时存到内存里,以备后面的代码继续调用,这几个名字的学名就叫做“变量” 用法: name = 'zzx' 其中name ...

  9. 2019年微服务5大趋势,你pick哪个?

    2018年对于微服务来说是非常重要的一年,这一年Service Mesh开始崭露头角,解决服务间复杂的通信问题,这一年很多国内互联网公司已经有了较为成熟的微服务实践案例,网易云主办的微服务实践沙龙中也 ...

  10. AJPFX:什么是外汇交易

    外汇交易是对货币对的一种买卖,是以一个国家货币与另外一个国家货币进行交换,即您在买入一国货币的同时,您也卖出了另一国的货币.所以在外汇市场上,人们的交易对象就是“货币对“,比如欧元/美元,美元/日元, ...