Android中使用styles

在项目开发中经常遇到这样的问题,在很多的Activity中都会有标题栏,而这个标题栏通常拥有类似的效果,比如下面的截图所示。那么你需要统一控制标题栏中的文字的大小,字体的颜色等等。方便修改和维护。

 

 

1.在res/values/styles.xml 中为每个控件编写style:

Java代码   收藏代码
  1. <resources xmlns:android="http://schemas.android.com/apk/res/android">  
  2.   
  3.     <!--  
  4.         Base application theme, dependent on API level. This theme is replaced  
  5.         by AppBaseTheme from res/values-vXX/styles.xml on newer devices.  
  6.   
  7.     -->  
  8.     <style name="AppBaseTheme" parent="android:Theme.Light">  
  9.         <!--  
  10.             Theme customizations available in newer API levels can go in  
  11.             res/values-vXX/styles.xml, while customizations related to  
  12.             backward-compatibility can go here.  
  13.   
  14.         -->  
  15.     </style>  
  16.   
  17.     <!-- Application theme. -->  
  18.     <style name="AppTheme" parent="AppBaseTheme">  
  19.         <!-- All customizations that are NOT specific to a particular API-level can go here. -->  
  20.     </style>  
  21.   
  22.     <!-- 页面头部  返回按钮样式 -->  
  23.     <style name="header_button_back" parent="@android:style/TextAppearance.Widget.Button">  
  24.         <item name="android:textSize">@dimen/header_text_size</item>  
  25.         <item name="android:textColor">@color/header_button_text_color</item>  
  26.         <item name="android:layout_height">@dimen/header_button_height</item>  
  27.         <item name="android:layout_width">@dimen/header_button_back_width</item>  
  28.         <item name="android:background">@drawable/btn_back_selector</item>  
  29.         <item name="android:text">返回</item>  
  30.         <item name="android:layout_marginLeft">8dip</item>  
  31.         <item name="android:paddingLeft">8dp</item>  
  32.        <item name="android:layout_gravity">center</item>   
  33.     </style>  
  34.       
  35.       
  36.     <!-- 页面头部  标题样式 -->  
  37.     <style name="header_text_view" parent="@android:style/TextAppearance.Widget.TextView">  
  38.         <item name="android:textSize">@dimen/header_title_text_size</item>  
  39.         <item name="android:textColor">@color/header_button_text_color</item>  
  40.         <item name="android:layout_height">@dimen/header_button_height</item>  
  41.         <item name="android:layout_width">0dp</item>  
  42.         <item name="android:layout_weight">1</item>  
  43.         <item name="android:gravity">center</item>   
  44.         <item name="android:text">标题</item>  
  45.     </style>   
  46.       
  47.       
  48.      <!-- 页面头部  操作按钮样式 -->  
  49.     <style name="header_button_operate" parent="@android:style/TextAppearance.Widget.Button">  
  50.        <item name="android:textSize">@dimen/header_text_size</item>  
  51.         <item name="android:textColor">@color/header_button_text_color</item>  
  52.         <item name="android:layout_height">@dimen/header_button_height</item>  
  53.         <item name="android:layout_width">wrap_content</item>  
  54.         <item name="android:layout_marginRight">5dp</item>  
  55.         <item name="android:layout_marginLeft">9dip</item>  
  56.         <item name="android:text">操作</item>   
  57.         <item name="android:layout_gravity">center</item>   
  58.     </style>  
  59.       
  60.       
  61.       <!-- 页面头部 容器样式 -->  
  62.     <style name="header_linear_layout">   
  63.         <item name="android:layout_width">fill_parent</item>  
  64.         <item name="android:layout_height">@dimen/header_height</item>  
  65.         <item name="android:background">@drawable/bg_navbar_no_line</item>   
  66.         <item name="android:gravity">center</item>   
  67.            
  68.     </style>   
  69.   
  70. </resources>  
 

2.在activity的布局文件中利用这些style:

 

Java代码   收藏代码
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <!-- HeadBar  
  8.          这样的话,你可以很容易地定义一个titlebar的样式,方便使用和统一修改。  
  9.          如果需要自定义可以在下面设置自己的属性,能够覆盖style中的属性,比如你可以设置layout_margin等等,能够覆盖style中的layout_margin  
  10.      -->  
  11.   
  12.     <LinearLayout  
  13.         style="@style/header_linear_layout"  
  14.         android:layout_alignParentTop="true" >  
  15.   
  16.         <Button  
  17.             android:id="@+id/btn_back"  
  18.             style="@style/header_button_back"  
  19.              />  
  20.   
  21.         <TextView  
  22.             style="@style/header_text_view"  
  23.             android:text="我的个人中心" />  
  24.   
  25.         <Button  
  26.             android:id="@+id/btn_operate"  
  27.             style="@style/header_button_operate"  
  28.             android:visibility="invisible" />  
  29.     </LinearLayout>  
  30.   
  31. </RelativeLayout>  

 

 

在你需要Titlebar的Activity的布局文件中使用上面的布局,就可以达到很方便控制所有控件属性的目的了。当然这也不妨碍你自定义某个控件的属性,因为你自定义的属性能够覆盖style中的属性。

 

 

上面style文件中用到的dimen这里也贴出来:res/values/dimens.xml

 

Java代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <dimen name="header_height">50dip</dimen>  
  5.     <dimen name="header_button_height">50dip</dimen>  
  6.     <dimen name="header_text_size">16sp</dimen>  
  7.     <dimen name="header_title_text_size">22sp</dimen>  
  8.     <dimen name="header_button_back_width">60dp</dimen>  
  9.     <dimen name="header_tab_width">80dp</dimen>  
  10.     <dimen name="header_tab_group_width">162dp</dimen>  
  11.   
  12. </resources>  
 

同样的color:res/values/colors.xml :

 

Java代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <color name="act_main_item_text_color">#505355</color>  
  5.     <color name="header_button_text_color">#e5e9ec</color>  
  6.       
  7. </resources>  
 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值