Android系统中ActionBar默认的布局不美观且难于控制,通过为ActionBar自定义布局的方式可以灵活控制ActionBar。

自定义Activity主题和ActionBar样式

在新建的android工程的res/values/styles.xml添加自定义ActionBar样式的代码和自定义Activity主题的代码,并在AndroidMainfest.xml中给需要使用该自定义ActionBar的Activity中加入该主题:

(1)自定义ActionBar样式代码如下:

   <!-- 自定义ActionBar样式,重设ActionBar背景,隐藏actionbar左侧的应用图标和标题 -->

    <style name="actionbar_def_style" parent="@android:style/Widget.Holo.Light.ActionBar">

        <!-- 在样式里面直接替换掉actionbar的背景,避免在有的窗口进入慢时导致进入时显示系统默认的actionbar -->  

        <item name="android:background">@drawable/actionbar_background</item>

        <!-- 在样式里面去掉acionbar的应用图标和标题 -->

        <item name="android:displayOptions">useLogo</item>

    </style>

(2)自定义Activity主题代码如下:

<!-- 自定义窗口样式,需要带ActionBar -->

    <style name="ActivityTheme_Light" parent="android:Theme.Holo.Light">

        <item name="android:actionBarStyle">@style/actionbar_def_style</item> 

    </style>

(3)在AndroidManifest.xml文件中需要使用该自定义ActionBar的Activity中加入该主题如下:

   <activity

            android:name="com.zmy.actionbarstyle.DiyActionBarActivity"

            android:theme="@style/ActivityTheme_Light"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

在Activity中加载ActionBar的自定义布局

(1)ActionBar加载自定义布局的代码封装如下:

/**

* 设置ActionBar的布局

* @param layoutId 布局Id

* 

* */

public void setActionBarLayout( int layoutId ){

    ActionBar actionBar = getActionBar( );

    if( null != actionBar ){

        actionBar.setDisplayShowHomeEnabled( false );

        actionBar.setDisplayShowCustomEnabled(true);

        LayoutInflater inflator = (LayoutInflater)   this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View v = inflator.inflate(layoutId, null);

        ActionBar.LayoutParams layout = new     ActionBar.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

        actionBar.setCustomView(v,layout);

    }

}

(2)在onCreate()方法中调用该方法,并传入自定义的布局文件ID:

@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_diy_action_bar);

    // 自定义actionbar的布局

    setActionBarLayout( R.layout.actionbar_port_layout );

}

(3)自定义布局文件“actionbar_port_layout.xml ”代码如下:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="63.0dp"

    android:id="@+id/actionbarLayoutId"

    android:background="@drawable/actionbar_background">

    <ImageButton

        android:id="@+id/menuBtnId"

        android:layout_marginLeft="12.0dp"

        android:layout_marginTop="12.0dp"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:background="@drawable/menu"

        android:contentDescription="@string/app_name"

        android:onClick="onClick"

        />

    <ImageButton 

        android:id="@+id/noteBtnId"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginTop="12.0dp"

        android:background="@drawable/notes"

        android:contentDescription="@string/app_name"

        android:layout_marginRight="12.0dp"

        android:layout_alignParentRight="true"

        android:onClick="onClick"

        />

    <ImageButton 

        android:id="@+id/editBtnId"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginTop="12.0dp"

        android:background="@drawable/edit"

        android:layout_toLeftOf="@id/noteBtnId"

        android:layout_marginRight="12.0dp"

        android:contentDescription="@string/app_name"

        android:onClick="onClick"

        />

    <ImageButton 

        android:id="@+id/downloadBtnId"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginTop="12.0dp"

        android:background="@drawable/download"

        android:layout_toLeftOf="@id/editBtnId"

        android:layout_marginRight="12.0dp"

        android:contentDescription="@string/app_name"

        android:onClick="onClick"

        />

</RelativeLayout>

在Action中监听ActionBar中按钮的点击事件

/**

* 实现onClick方法,在这里面监听actionbar中按钮的点击事件 

* 

* */

public void onClick( View v ){

    switch( v.getId( ) ){

    case R.id.menuBtnId:{

        showToast( this, "menuBtn" );

    }

    break;

    case R.id.noteBtnId:{

         showToast( this, "noteBtn" );

    }

    break;

    case R.id.downloadBtnId:{

         showToast( this, "downloadBtn" );

    }

    break;

    case R.id.editBtnId:{

        showToast( this, "editBtn" );

    }

    break;

    default:{

    }

    break;

    }

}

源码:http://download.csdn.net/detail/zmywly/6922375

AcitonBar 自定义布局的更多相关文章

  1. 干货之UIButton的title和image自定义布局

    当需要实现一个自定义布局图片和标题的按钮时候,不知道有多少少年直接布局了UIButton,亦或是自定义一个UIView,然后以空白UIButton.UILabel.UIImageVew作为subVie ...

  2. SharePoint 2013 设置自定义布局页

    在SharePoint中,我们经常需要自定义登陆页面.错误页面.拒绝访问等:不知道大家如何操作,以前自己经常在原来页面改或者跳转,其实SharePoint为我们提供了PowerShell命令,来修改这 ...

  3. Collection View 自定义布局(custom flow layout)

    Collection view自定义布局 一般我们自定义布局都会新建一个类,继承自UICollectionViewFlowLayout,然后重写几个方法: prepareLayout():当准备开始布 ...

  4. iOS-UICollectionView自定义布局

    UICollectionView自定义布局 转载: http://answerhuang.duapp.com/index.php/2013/11/20/custom_collection_view_l ...

  5. 详细分享UICollectionView的自定义布局(瀑布流, 线性, 圆形…)

    前言: 本篇文章不是分享collectionView的详细使用教程, 而是属于比较’高级’的collectionView使用技巧, 阅读之前, 我想你已经很熟悉collectionView的基本使用, ...

  6. OC - 31.通过封装的自定义布局快速实现商品展示

    概述 实现效果 设计思路 采用MVC架构,即模型—视图-控制器架构 使用MJExtension框架实现字典转模型 使用MJRefresh框架实现上拉和下拉刷新 上拉刷新,加载新的数据 下拉刷新,加载更 ...

  7. OC - 30.如何封装自定义布局

    概述 对于经常使用的控件或类,通常将其分装为一个单独的类来供外界使用,以此达到事半功倍的效果 由于分装的类不依赖于其他的类,所以若要使用该类,可直接将该类拖进项目文件即可 在进行分装的时候,通常需要用 ...

  8. OC - 29.自定义布局实现瀑布流

    概述 瀑布流是电商应用展示商品通常采用的一种方式,如图示例 瀑布流的实现方式,通常有以下几种 通过UITableView实现(不常用) 通过UIScrollView实现(工作量较大) 通过UIColl ...

  9. Android:创建可穿戴应用 - 自定义布局

    创建自定义布局(Creating Custom Layouts) 本文将介绍如何创建自定义通知以及使用可穿戴UI库来创建自定义布局你同时还需要了解可穿戴设计准则(Wear Design Princip ...

随机推荐

  1. spring mvc model.addAttribute页面c:forEach取不到

    昨天和今天都在解决一个问题,即: @RequestMapping(value = "/listAccounts", method = RequestMethod.GET) publ ...

  2. heap(堆)和stack(栈)的区别

    heap是堆,stack是栈 stack的空间由操作系统自动分配/释放,heap上的空间手动分配/释放. stack空间有限,heap是很大的自由存储区 C中的malloc函数分配的内存空间即在hea ...

  3. 桌面环境与桌面搜索Desktop Search tools

    最近一段时间工作重心都将放在Linux下Desktop search(桌面搜索)框架的研发上.因此对desktop search进行了初步的调研.本文将从下面三个方面展开: Linux桌面环境(Des ...

  4. 36th成都区域赛网络赛 hdoj4039 The Social Network(建图+字符串处理)

    这题是某年成都区域赛网络赛的一题. 这题思路非常easy,可是从时间上考虑,不妨不要用矩阵存储,我用的链式前向星. 採用线上查询.利用map对字符串编号,由于非常方便.要推荐的朋友,事实上就是朋友的朋 ...

  5. Windows API一日一练(55)FlushFileBuffers和SetFilePointer函数

    在PC硬件体系结构里,速度最快的存储器是CPU里面的寄存器,接着到二级缓存,再到系统RAM内存,最后才到硬盘.因为这种体系结构,就决定了操作系统对文件的操作方式,或者说是最优化的算法.比方操作系统接收 ...

  6. DHTML【1】

       什么是DHTML呢?英文全称(Dynamic Hypertext Markup Language),即动态超文本标记语言,DHTML不是一个独立的新技术,而是多种技术的综合,它能使HTML变的更 ...

  7. 根据goodsId获得相关商品的列表

    List<Goods> goodsList = goodsDetailService.getGoodsListByproductId(productId); for (Goods good ...

  8. EventBus 事件总线 案例

    简介 地址:https://github.com/greenrobot/EventBus EventBus是一个[发布 / 订阅]的事件总线.简单点说,就是两人[约定]好怎么通信,一人发布消息,另外一 ...

  9. JavaScript网页制作特效

    一.什么是JavaScript? 网页交互特效的脚本语言. 特效 二.BOM对象 能够使得JavaScript和浏览器进行对话. 主要是使用Window对象进行操作. History对象:历史,可以实 ...

  10. memcache缓存命中深入理解转载

    http://www.iteye.com/topic/225692 memcache的方法有 add,set,replace,get,delete,getstats,increment,decreme ...