android 使用android.support.v7 添加ActionBar
来源:互联网 发布:php 编译扩展 编辑:程序博客网 时间:2024/05/10 03:54
当需要在 android 7或更高的版本使用 ActionBar,则可以通过继承ActionBarActivity来实现,
网上有一个开源项目来兼容老版本显示ActionBar的效果:ActionBarSherlock。
使用android.support.v7 来实现ActionBar感觉会更简单一些。
android.support.v7 有三个jar文件可供使用,对于ActionBar我们需要使用android-support-v7-appcompat.jar
路径:...\sdk\extras\android\support\v7\appcompat\libs
但是直接把这个jar 导入项目是不可以使用的,具体的步骤:
1. 把appcompat 工程导入eclipse [appcompat是一个 library project] ,路径:...\sdk\extras\android\support\v7\appcompat\
导入之后的项目:
2.导入appcompat工程的时候,记得要 Copy projects into workspace
3.创建你自己的工程,如myDemo,右键单击该工程,选择Properties
在弹出的对话框中,选择Android选项卡,然后点击Add按钮,添加appcompat工程。
然后 OK 就可以了,我们的工程就引用了 support V7库工程了。
4.在manifest文件中,为你使用了ActionBar的actitity添加theme属性,这个theme必须是 @style/Theme.AppCompat 或它的子类
如:
5.在res/menu目录下,修改Menu对应的xml文件:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:alpha="http://schemas.android.com/apk/res-auto" > <item android:id="@+id/action_settings" android:orderInCategory="100" android:icon="@drawable/ic_launcher" alpha:showAsAction="ifRoom|withText" android:title="@string/action_settings"/></menu>
需要注意的是:
xmlns:alpha="http://schemas.android.com/apk/res-auto" 和 alpha:showAsAction="ifRoom|withText"
他们的 alpha 要一致哦,相当于自定义一个命名空间,名字可以随意,但要一致。
感觉这样的含义:如果直接使用android:showAsAction,但对于早期的版本来说android命名空间下不存在这个属性,就不能看到ActionBar的效果,而这个属性的定义存在于support V7 里面,所以可以兼容之前的版本。
6.继承 ActionBarActivity 处理ActionBar
import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.view.Menu;public class MainActivity extends ActionBarActivity{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
- android 使用android.support.v7 添加ActionBar
- android 使用android.support.v7 添加ActionBar
- android 使用android.support.v7 添加ActionBar
- Android Support V7 包中 ActionBar的使用 (3) 为ActionBar添加Up导航功能
- 添加Android Support V7
- android添加support v7
- Android Support V7 包中 ActionBar的使用 (1) 添加Action Item
- Android Support V7 包中 ActionBar的使用 (1) 添加Action Item
- Android Support V7 包中 ActionBar的使用
- android-support-v7使用
- android.support.v7包添加
- Android Support V7 包中 ActionBar的使用 (2) 分离式 ActionBar
- ActionBar(1) 告别ActionBarSherlock,使用android.support.v7.app.ActionBar来代替
- 使用android support V7 Demo
- 如何使用android-support-V7包中ActionBar(Eclipse版)
- 如何使用android-support-V7包中ActionBar(Eclipse版)
- 如何使用android-support-V7包中ActionBar(Eclipse版)
- ActionBar-在Google官方android-support-v7包中的一些使用
- 自定义Adapter
- Sealed
- 如何使用FacesContext类
- C#实现的内存分页机制的一个实例
- asp.net测试时 使用configurationmanager时报错:类型初始值设定项引发异常
- android 使用android.support.v7 添加ActionBar
- Openwrt 单wan口N 拨教程
- 根据指定电话号码得到通讯录上的姓名
- 模态对话框和非模态对话框
- hdu1281结题报告
- 手动配置WCF宿主的.config文件遇到的几种错误
- hibernate学习笔记01----hibernate介绍
- 黑马程序员——网络编程
- HDU 3516 Tree Construction (四边形优化DP)