Android ActionBar自定义
关于自定义的ActionBar的实现过程,这里做下笔记以供之后查看。
1.默认状态
使用Android Studio新建一个名为“ActionBar”的应用,默认actionbar如图(1)所示。

图1
2.自定义
自定义actionbar可以通过以下代码实现:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); ActionBar actionBar = getActionBar();
if (actionBar != null) {
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); //Enable自定义的View
actionBar.setCustomView(R.layout.actionbar_custom);//设置自定义的布局:actionbar_custom
}
}
其中ActionBar布局文件actionbar_custom.xml如下:
<?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="@dimen/actionbar_hight"
android:background="@color/customBackgroung"> <TextView
android:id="@+id/action_bar_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Custom ActionBar"
android:textColor="@android:color/black"
android:textSize="16sp"
android:gravity="center"/>
</LinearLayout>
但是真正显示自定义ActionBar如图(2),布局周围还有Margin,这个应该是需要修改Theme中对应项。

图2
<!-- 自定义的ActionBar Style-->
<style name="CustomActionBarStyle" parent="@android:style/Widget.DeviceDefault.Light.ActionBar">
<item name="android:contentInsetEnd">0dp</item><!--右边距为0-->
<item name="android:contentInsetStart">0dp</item><!--左边距为0-->
</style>
<!-- 自定义的Theme-->
<style name="CustomTheme" parent="@android:style/Theme.DeviceDefault.Light.DarkActionBar">
<item name="android:colorPrimary">@color/colorPrimary</item>
<item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="android:colorAccent">@color/colorAccent</item>
<item name="android:actionBarSize">@dimen/actionbar_hight</item><!--设置ActionBar的高度-->
<item name="android:actionBarStyle">@style/CustomActionBarStyle</item>
<item name="android:statusBarColor">@color/customBackgroung</item><!--效果和colorPrimary一样-->
<item name="android:windowLightStatusBar">true</item><!--设置StatusBar上Icon高亮-->
<item name="android:windowTranslucentNavigation">false</item>
</style>
最后在AndroidManifest.xml中将自定义的主题应用到对应的Activity or Application中。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.steve.customactionbar"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true">
<activity android:name=".MainActivity"
android:theme="@style/CustomTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>
最终就可以得到想要的结果,如图3。

图3
ps:关于colorPrimary & colorPrimaryDark & colorAccent颜色的作用,如图4所示:

图4
参考文章:http://www.cnblogs.com/tianzhijiexian/p/4081562.html
Android ActionBar自定义的更多相关文章
- Android ActionBar详解
Android ActionBar详解 分类: Android2014-04-30 15:23 1094人阅读 评论(0) 收藏 举报 androidActionBar 目录(?)[+] 第4 ...
- Android 自定义View修炼-【2014年最后的分享啦】Android实现自定义刮刮卡效果View
一.简介: 今天是2014年最后一天啦,首先在这里,我祝福大家在新的2015年都一个个的新健康,新收入,新顺利,新如意!!! 上一偏,我介绍了用Xfermode实现自定义圆角和椭圆图片view的博文& ...
- Android ActionBar应用实战,高仿微信主界面的设计
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/26365683 经过前面两篇文章的学习,我想大家对ActionBar都已经有一个相对 ...
- Android ActionBar完全解析,使用官方推荐的最佳导航栏(下) .
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/25466665 本篇文章主要内容来自于Android Doc,我翻译之后又做了些加工 ...
- ActionBar 自定义布局定义
ActionBar 自定义布局定义 Android系统中ActionBar默认的布局不美观且难于控制,通过为ActionBar自定义布局的方式可以灵活控制ActionBar. 效果: 工具/原料 ...
- Android RatingBar 自定义样式
Android RatingBar 自定义样式 1.先定义Style: <style name="RadingStyle" parent="@android:sty ...
- Android实现自定义带文字和图片的Button
Android实现自定义带文字和图片的Button 在Android开发中经常会需要用到带文字和图片的button,下面来讲解一下常用的实现办法. 一.用系统自带的Button实现 最简单的一种办法就 ...
- Android Studio 自定义debug签名文件keystore
Android Studio 自定义debug签名文件keystore
- Android读取自定义View属性
Android读取自定义View属性 attrs.xml : <?xml version="1.0" encoding="utf-8"?> < ...
随机推荐
- Android TextView实现跑马灯
TextView实现跑马灯的效果:例子一: 这个例子可以解决给一个TextView实现跑马灯的效果,但是不能解决给所有的TextView实现跑马灯的效果. <TextView android:l ...
- ansible批量验证密码
author: headsen chen date: 2018-08-31 20:45:49 综合比较salt-ssh,ansible .sshpass等批量验证100台机器的密码,最好推荐用ans ...
- ansible的入门级使用
author: headsen chen date: 2018-08-02 11:46:35 1,ansible的安装 yum install epel-release yum -y i ...
- EUI组件之HScrollBar VScrollBar (动态设置滑块图片)
一.常规使用 官网教程里没有这个组件的使用方法 这个组件配合Scroller使用 拖动一个scroller到exml上.scroller上已经默认存在了HScrollBar和VScrollBar 当图 ...
- onethink插件控制器如何访问?
具体路由分析就不说啦!就是那样.这里我只是方便访问来做一个记录,方便复制粘贴访问: 例如:新增一个Baoming的插件: 那么如何,访问这个控制里面方法呢? 第一种情况:这个控制器使用的是Admin模 ...
- OneThink生成分类树方法(list_to_tree)使用!
具体方法: Application / Common / Common / function.php 下的 224行: function list_to_tree($list, $pk='id', $ ...
- 处理URL传递中文乱码问题
在网上搜了很多资料都没有搞定,一般都有以下几种说法: 方法1:在后台中先获得字符串的iso-8859-1编码形式数组,再使用此数组实例一个UTF-8编码形式String类型字符串. 页面提交的url为 ...
- Java 泛型 详解
一.什么是泛型 本质而言,泛型指的是参数化的类型.参数化的类型的重要性是:它能让你创建类.接口和方法,由它们操作的数据类型被指定为一个参数.操作参数化类型的类.接口或方法被称为泛型,如泛型类或泛型方法 ...
- 170612、web如何项目优化
Web性能优化分为服务器端和浏览器端两个方面. 一.浏览器端,关于浏览器端优化,分很多个方面1.压缩源码和图片JavaScript文件源代码可以采用混淆压缩的方式,CSS文件源代码进行普通压缩,JPG ...
- CodeForces 25C(Floyed 最短路)
F - Roads in Berland Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I6 ...