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

做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. 【mac上安装&配置&使用git】

    转自:https://www.jianshu.com/p/7edb6b838a2e 目录 安装git 创建ssh key.配置git 提交本地项目到GitHub 一.安装Git MAC 上安装Git主 ...

  2. easyui combobox下拉框文字超出宽度有横向滚轮

    //下拉框显示横向滚轮 $(".combo").mouseenter(function(){ $(this).prev().combobox("showPanel&quo ...

  3. Linux命令列内容

    命令列内容: 一般模式 移动光标 [ctrl]+[f] 屏幕[向前]移动一页 [ctrl]+[b] 屏幕[向后]移动一页 0 这是数字0:移动到这一行的最前面字符处 $ 移动到这一行的最后面字符处 G ...

  4. java编译时出现——注:使用了未经检查或不安全的操作。注:有关详细信息,请使用 -Xlint:unchecked 重新编译

    网上说是泛型问题 private List<Product> products = new ArrayList<Product>(); 这种用法绝对没错!(因为是照着书写的)在 ...

  5. spring整合kafka(配置文件方式 生产者)

    Kafka官方文档有   https://docs.spring.io/spring-kafka/reference/htmlsingle/ 这里是配置文件实现的方式 先引入依赖 <depend ...

  6. 转载:C# 将引用的DLL文件放到指定的目录下

    当软件引用的DLL比较多的时候,全部的DLL都放在exe同目录下,显得比较乱,如果能把dll放到响应的文件夹下面,就方便很多 下面是解决该问题的一种方法: 右键点击项目:属性->设置,项目会生成 ...

  7. SpringBoot集成redis,使用@Cachexxxx

    一.引入相关依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId& ...

  8. TensorFlow环境搭建

    1.使用pip安装TensorFlow 第一步安装pip: 先安装python 官网下载地址https://www.python.org在里面选择适合自己的版本 安装python的过程中pip也会随之 ...

  9. VIP之Switch

    Switch II 最大能连接12路输入与12路输出 不能合并数据数 每个输入可以驱动多个输出 每个输出只能被一个输入驱动 当输入没有连接到输出时,可以禁止掉 每个被禁止的输入可以设置成停止或者消耗模 ...

  10. EF6学习笔记(六) 创建复杂的数据模型

    EF6学习笔记总目录:ASP.NET MVC5 及 EF6 学习笔记 - (目录整理) 本篇原文地址:Creating a More Complex Data Model 本篇讲的比较碎,很多内容本人 ...