在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. 【面试虐菜】—— JAVA面试题(3)

    1 throws与throw的区别 解析:throws和throw是异常处理时两个常见的关键字,初级程序员常常容易正确理解throw和throws的作用和区别,说明已经能比较深入理解异常处理.Thro ...

  2. 把数组排成最小的数/1038. Recover the Smallest Number

    题目描述 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323.   Give ...

  3. 介绍一下linux的文件系统

    (1)/bin:该目录用于存放用户命令. 目录 /usr/bin 中也存放了一些用户命令.(2)/sbin:该目录用于存放许多系统命令,例如 shutdown.目录 /usr/bin 中也包括了许多系 ...

  4. 通过Linux命令过滤出binlog中完整的SQL语句

    DB:5.6.16CentOS:CentOS release 6.3 (Final) 当insert语句通过空格跨行输入的时候,如何提取完整的insert语句! 创建一个空表:mysql> cr ...

  5. backbone前端基础框架搭建

    前端站点名为:site: 前端框架分为:css.js和img,框架的核心在js文件夹下: js中包括collections.models.views.lib和一个app入口js

  6. HotSpot Builder Utility安装指南

    系统需求硬件- 一台带有1个以太网卡的电脑(宿主机)- 一个无线路由器 软件- VirtualBox 4.1或更高的版本.下载网址:http://www.virtualbox.org/- 我们提供的最 ...

  7. awk简明教程

    我在这里的教程并不想面面俱到,全是示例,基本无废话. 我只想达到两个目的: 1)你可以在乘坐公交地铁上下班,或是在坐马桶拉大便时读完(保证是一泡大便的工夫). 2)我只想让这篇博文像一个火辣的脱衣舞女 ...

  8. Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

    Exception in thread "main" java.lang.OutOfMemoryError: Java heap space解决方法 问题描述 Exception ...

  9. AJAX请求遭遇未登录和Session失效的解决方案

    使用技术:HTML + Servlet + Filter + jQuery 一般来说我们的项目都有登录过滤器,一般请求足以搞定.但是AJAX却是例外的,所以解决方法是设置响应为session失效. 一 ...

  10. Oracle 存储过程实例2

    --创建存储过程 CREATE OR REPLACE PROCEDURE xxxxxxxxxxx_p ( --参数IN表示输入参数,OUT表示输入参数,类型可以使用任意Oracle中的合法类型. is ...