安卓入门-UI 之自定义actionbar样式

首先样式的修改组要是在res/values/styles文件中做修改:

大上分为三个步骤:

1、在如下代码中添加:<item name="android:actionBarStyle" >@style/myActionBarStyle</item>

[html]  view plain  copy
  1. <style name="AppTheme" parent="AppBaseTheme">  
[java]  view plain  copy
  1. <!-- All customizations that are NOT specific to a particular API-level can go here. -->  
  2.         
  3. </style>  

2、增加第一步中的被引用样式的具体内容

[java]  view plain  copy
  1. <style name="myActionBarStyle" parent="android:Widget.ActionBar" >                  
[java]  view plain  copy
  1. <item name="android:background"></span>写上要的背景颜色</item>     
[java]  view plain  copy
  1. <item name="android:titleTextStyle">@style/titleStyle</item> 此处为你要的标题颜色                    
  2.  <item name="android:backgroundSplit">写上要的背景颜色</item>此处为底部操作栏的颜色(有些时候需要显示底部操作栏比如魅族手机应特别注意此项)  
  3. </style>  

3、同样的第三步增加第二步中引用样式的具体内容:

[java]  view plain  copy
  1. <style name="titleStyle">                        
[java]  view plain  copy
  1. <item name="android:textSize">写上文字大小</item>                   
[java]  view plain  copy
  1. <item name="android:textColor">写上文字颜色</item>              
[java]  view plain  copy
  1. </style>   

这样就完成了actionbar的样式的基本自定义,较为复杂的这里就不写了。后了本人还发现现在许多app都要求actionbar的标题要居中去显示,下面贴出一种方法解决这个问题

1、自己写一个布局文件

[html]  view plain  copy
  1. <span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:layout_width="match_parent"    
  4.     android:layout_height="match_parent"    
  5.     android:gravity="center_vertical"    
  6.     android:paddingLeft="10dp"    
  7.     android:paddingRight="10dp" >    
  8.     <TextView   
  9.         android:id="@+id/anctionbar_title"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:textSize="25sp"  
  13.         android:textColor="#FFFFFFFF"  
  14.          android:layout_centerInParent="true"  
  15.         android:text="标题"  
  16.         />  
  17. </RelativeLayout>  
  18. </span>  



2、在actionBar的显示处写下如下代码

//隐藏掉系统本身的标题

getActionBar().setDisplayShowTitleEnabled(false);
// 显示自定义视图
getActionBar().setDisplayShowCustomEnabled(true);
//此处自定义了一个actionbar为了让
标题居中显示
actionbarLayout = LayoutInflater.from(this).inflate(R.layout.actionbar_layout, null);
getActionBar().setCustomView(
actionbarLayout,
new ActionBar.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
ActionBar.LayoutParams mP = (ActionBar.LayoutParams) actionbarLayout
.getLayoutParams();
mP.gravity = mP.gravity & ~Gravity.HORIZONTAL_GRAVITY_MASK
| Gravity.CENTER_HORIZONTAL;

getActionBar().setCustomView(actionbarLayout, mP);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值