【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 ...
随机推荐
- 【转载】在MySQL登录时出现Access denied for user 'root'@'localhost' (using password: YES) 拒绝访问,并可修改MySQL密码
在MySQL登录时出现Access denied for user 'root'@'localhost' (using password: YES) 拒绝访问,并可修改MySQL密码 2018年08月 ...
- vue修改端口号
vue修改端口号 默认端口号 修改端口号 重新启动项目 npm run dev
- 3T - A1 = ?
有如下方程:A i = (A i-1 + A i+1)/2 - C i (i = 1, 2, 3, .... n). 若给出A 0, A n+1, 和 C 1, C 2, .....C n. 请编程计 ...
- DB2 键约束操作
DB2 键约束操作 今天遇到一个问题,想要删掉一个表中的某列的唯一约束,google了一下,搜出来许许多多,找到能用的,记下来总结如下. 命令如下 select constname, tabname, ...
- Arrays工具类和Collections工具类
集合知识点总结 Arrays工具类 .binarySearch() .sort() .fill() //填充 int[] array = new int[10]; Arrays.fill(array, ...
- 2019.02.15 bzoj5210: 最大连通子块和(链分治+ddp)
传送门 题意:支持单点修改,维护子树里的最大连通子块和. 思路: 扯皮: bzojbzojbzoj卡常差评. 网上的题解大多用了跟什么最大子段和一样的转移方法. 但是我们实际上是可以用矩阵转移的传统d ...
- LOJ-10106(有向图欧拉回路的判断)
题目链接:传送门 思路: (1)将每个单词视为有向路径,单词的起始字母是起始节点,末尾字母是终止节点,然后找由字母建立的有向图 是否是欧拉图或者半欧拉图. (2)先用并查集判断是否连通,再判断入度与出 ...
- 部署eclipse项目到tomcat
1.为了以防万一,将本地tomcat版本及其jdk版本与服务器上的版本最好是相同的 2.在本地eclipse下运行项目即可发布(注意(1)数据库连接的是服务器数据库还是本地数据库(2)运行项目前先cl ...
- mac开发常用工具和插件记录
1.alfred 是 Mac 系统上一款专注于效率提升的著名应用,它能帮你快速打开网页.快速进行自定义搜索.查看剪贴板历史.快速查询单词等等.Alfred 提供的功能虽然很多,但目的只有一个 —— 那 ...
- java批量将多文件打包成zip格式
public void createzip(){ List<File> nFileList = new ArrayList<File>(); nFileList.add(new ...