在android的actionBar中,actionBar的视图是固定的,左边是程序的图标和title,右边是添加的menuItem,如果想要定制actionbar中的view就要自定义视图。

首先要定义actionbar的xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="@+id/layout01"
> <ImageButton android:id="@+id/left"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="@android:color/transparent"
android:src="@drawable/show_left"
android:layout_gravity="left|center_vertical"
android:paddingLeft="8dip"
android:clickable="true"/> <TextView android:id="@+id/tv"
android:background="@android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="首页"
android:textColor="@android:color/white"
android:textSize="18dip"
android:paddingTop="2dip"
android:layout_gravity="center"/> <ImageButton android:id="@+id/more"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="@android:color/transparent"
android:src="@drawable/cross"
android:paddingRight="8dip"
android:clickable="true"
android:layout_gravity="right|center_vertical"/>
</FrameLayout>

然后在onCreateMenuItem设置actionBar的样式,一定要设 置 actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM)和 actionBar.setDisplayShowCustomEnabled(true);

public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu); actionBar=this.getActionBar();
ActionBar.LayoutParams params=new ActionBar.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT,Gravity.CENTER );
View view=LayoutInflater.from(this).inflate(R.layout.actionbar, null);
actionBar.setCustomView(view,params);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setDisplayShowCustomEnabled(true); ImageButton more=(ImageButton) this.findViewById(R.id.more);
ImageButton left=(ImageButton) this.findViewById(R.id.left);
left.setOnTouchListener(new MyTouchListener());
more.setOnTouchListener(new MyTouchListener());
return true; } }
class MyTouchListener implements OnTouchListener
{ @Override
public boolean onTouch(View view, MotionEvent arg1) {
if(arg1.getAction()==MotionEvent.ACTION_DOWN)
{
view.setBackgroundColor(Color.LTGRAY);
}
if(arg1.getAction()==MotionEvent.ACTION_UP)
{
view.setBackgroundColor(Color.TRANSPARENT);
}
return true;
}

android actionbar标题栏的更多相关文章

  1. Android应用:StatusBar状态栏、NavigationBar虚拟按键栏、ActionBar标题栏、Window屏幕内容区域等的宽高

    一.屏幕中各种栏目以及屏幕的尺寸 当我们需要计算屏幕中一些元素的高度时,或许需要先获取到屏幕或者各种栏目的高度,下面这个类包含了Status bar状态栏,Navigation bar虚拟按键栏,Ac ...

  2. [Xamarin.Android] ActionBar Tips

    [Xamarin.Android] ActionBar Tips ActionBar用途 快速搞懂 ActionBar的用途,可以参考下列文章: [Android]使用 ActionBarCompat ...

  3. Android ActionBar详解

    Android ActionBar详解 分类: Android2014-04-30 15:23 1094人阅读 评论(0) 收藏 举报 androidActionBar   目录(?)[+]   第4 ...

  4. Android ActionBar(转)

    本文内容 关于 ActionBar 必要条件 项目结构 环境 演示一:Action Bar 显示隐藏 演示二:Action Item 显示菜单选项 演示三:Action Home 启用“返回/向上”程 ...

  5. Android ActionBar应用实战,高仿微信主界面的设计

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/26365683 经过前面两篇文章的学习,我想大家对ActionBar都已经有一个相对 ...

  6. Android ActionBar完全解析,使用官方推荐的最佳导航栏(下) .

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/25466665 本篇文章主要内容来自于Android Doc,我翻译之后又做了些加工 ...

  7. Android ActionBar完全解析,使用官方推荐的最佳导航栏(上)

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/18234477 本篇文章主要内容来自于Android Doc,我翻译之后又做了些加工 ...

  8. [置顶] xamarin android自定义标题栏(自定义属性、回调事件)

    自定义控件的基本要求 这篇文章就当是自定义控件入门,看了几篇android关于自定义控件的文章,了解了一下,android自定义控件主要有3种方式: 自绘控件:继承View类,所展示的内容在OnDra ...

  9. Android ActionBar全然解析,使用官方推荐的最佳导航栏(上)

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/18234477 本篇文章主要内容来自于Android Doc.我翻译之后又做了些加工 ...

随机推荐

  1. iOS定位服务编程详解

    现在的移动设备很多都提供定位服务,使用iOS系统的iPhone.iPod Touch和iPad都可以提供位置服务,iOS设备能提供3种不同途径进行定位:Wifi, 蜂窝式移动电话基站, GPS卫星 i ...

  2. 命令行插入含有中文的sql文件,报错ERROR 1366 (HY000): Incorrect stringvalue:

    --以下是插入语句: insert into sms_inbox values('123456','123456', 'cd', sysdate(), '今天天 气很好', 1, sysdate(), ...

  3. 【转】- 使用T4模板批量生成代码

    前言 之前在 “使用T4模板生成代码 - 初探” 文章简单的使用了T4模板的生成功能,但对于一个模板生成多个实例文件,如何实现这个方式呢?无意发现一个解决方案 “MultipleOutputHelpe ...

  4. backup site collection

    http://stackoverflow.com/questions/5376380/sharepoint-2010-change-sitecollection-urlstsadm -o backup ...

  5. VS2013中设置大小写的快捷键

    1.我们在定义头文件时,通常需要定义: #ifndef  _MainMenu_H_#define  _MainMenu_H_ your code... #endif 我们需要将头文件名设置为大写的: ...

  6. error:LNK2005 已经在*.obj中定义

    为什么会出现这个错误??“error LNK2005: 已经在*.obj中定义”  编程中经常能遇到LNK2005错误——重复定义错误,其实LNK2005错误并不是一个很难解决的错误,弄清楚它形成的原 ...

  7. 兼容iOS 10 资料整理笔记-b

    原文链接:http://www.jianshu.com/p/0cc7aad638d9 1.Notification(通知) 自从Notification被引入之后,苹果就不断的更新优化,但这些更新优化 ...

  8. 输入adb shell 时 提示error: more than one device and emulator

    第一种情况:确实用多个设备或者模拟器 解决办法:(指定连接某一个设备或者模拟器) 1.获取模拟器/设备列表   adb devices 2.指定device来执行adb shell   adb -s ...

  9. 【读书笔记】Redis入门

    1:Redis概览 Remote Dictionary Server 远程字典服务 Redis是基于内存的存储 在一台普通的笔记本上,Redis每秒的读取速度可以达到10万 内存读取数据,断电的时候数 ...

  10. Linux "ls -l"文件列表权限详解

    ls Linux "ls -l"文件列表权限详解 1.使用 ls -l 命令 执行结果如下(/var/log) : drwxr-x--- root adm -- : apache2 ...