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"?> < ...
随机推荐
- 使用keytool生成密钥对
1.首先要用KeyTool工具来生成私匙库:(-alias别名 –validity 3650表示10年有效) keytool -genkey -alias privatekey -keystore p ...
- Python 流程控制:while
while 语法如下,当条件为假时循环才退出,否则会一直循环下去: while 条件: 执行语句 当条件为假时,才会执行else语句: while 条件: 执行语句 else: 执行语句
- docker search 详解
docker search 用于从 Docker Hub(https://hub.docker.com)中搜索指定的镜像,用法如下: [root@localhost ~]$ docker search ...
- oracle SUM函数
select change_type as change_type, sum(points1) as points from (select DECODE(p.change_type, , ' 兑换商 ...
- docker nginx mysql
docker run -p 9000:9000 --name myphp -v /docker/www/:/var/www/html/ -v /docker/php/php.ini:/usr/loca ...
- Spring学习笔记--声明一个简单的Bean
spring依赖的maven dependencyhttp://mvnrepository.com/artifact/org.springframework 在pom.xml中添加如下依赖: < ...
- PyQt4文件对话框QFileDialog
文件对话框允许用户选择文件或文件夹,被选择的文件可进行读或写操作. #!/usr/bin/python # -*- coding: utf-8 -*- import sys from PyQt4 im ...
- [原创]用Oreans UnVirtualizer还原VM代码
标题:[原创]用Oreans UnVirtualizer还原VM代码实验 作者:sungy 时间:2014-09-18 对VM一直很头痛,在逆向实践一般尽量想办法避开它,当黑盒来处理.有时候不面对她又 ...
- AndroidWear开发之开发环境[前奏]
上篇教程教的是如何下载最新的SDK http://www.cnblogs.com/bvin/p/3811751.html 一.Eclipse下的尝试 之前以为在Eclipse下把SDK,ADT更新一下 ...
- Thinkphp3.2 PHPexcel 导出
1 下载phpexecl 放入到tp里边. 路径如下:项目根目录\ThinkPHP\Library\Org\Util 2 PHP 代码部分 封装一个方法 private function getE ...