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"?> < ...
随机推荐
- AsycnTask
一.异步任务加载网络数据: 在Android中提供了一个异步任务的类AsyncTask,简单来说,这个类中的任务是运行在后台线程中的,并可以将结果放到UI线程中进行处理,它定义了三种泛型,分别是Par ...
- jsonObject的一些方法
1.从前端传过来的数字,默认是Integer类型不能直接用Long接收 错误写法: 报错:Exception in thread "main" java.lang.ClassCas ...
- JSP自定义标签rtexprvalue属性
rtexprvalue的全称是 Run-time Expression Value, 它用于表示是否可以使用JSP表达式.(比如EL表达式或OGNL表达式). 当在<attribute>标 ...
- java设计模式----外观模式(门面模式)
外观模式主要应用场景在于为复杂的子系统提供一个简单的接口,提高子系统的独立性. 创建DrawerOne类: package facade; public class DrawerOne { publi ...
- onethink插件控制器如何访问?
具体路由分析就不说啦!就是那样.这里我只是方便访问来做一个记录,方便复制粘贴访问: 例如:新增一个Baoming的插件: 那么如何,访问这个控制里面方法呢? 第一种情况:这个控制器使用的是Admin模 ...
- linux防火墙iptables
2.1 框架图 -->PREROUTING-->[ROUTE]-->FORWARD-->POSTROUTING--> mangle | mangle ^ mangle n ...
- Spring Security使用心得
某天,你的客户提出这样一个需求,在点击购买商品的时,如果用户没有注册,并且用户没有账号,这时用户去创建账户,然后要直接返回到想购买商品的付款页面.你会该如何基于Spring Security实现? S ...
- HDU 5652 India and China Origins(并查集)
India and China Origins Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- OGG双向复制
注意:在进行如下配置之前,先在源数据库(原来的目标数据库)端添加辅助的redolog配置: 1.SQL> alter database add supplemental log dat ...
- PHP通过curl模拟POST上传文件,5.5之前和之后的区别
首先先要着重提一下,只要是做和项目有关的开发,首先按把环境中各个服务的版本保持一致,否则出些莫名其妙的错我,让你百爪挠心却不知哪里的问题.这里就要说下curl_setopt($ch, CURLOPT_ ...