style的使用

 Style个人理解就是view的一些属性的集合,那么一系列view(例如TextVIew),只要是要该style那么就都有相同的内容,如 文字的大少,颜色等,方便修改

首先最基本的使用,多个textView都显示一样的颜色 跟文字大少等属性

   Sytle的定义:

[java]  view plain  copy
  1. <style  name="TextViewStyle1">  
  2.       <item name="android:textColor">@android:color/holo_red_light</item>  
  3.       <item name="android:textSize">40sp</item>  
  4.       <item name="android:layout_height">wrap_content</item>  
  5.       <item name="android:layout_width">200dp</item>  
  6.       <item name="android:background">#ffff00ff</item>  
  7.       <item name="android:gravity">center_horizontal</item>  
  8.   </style>  
这些属性都熟悉,使用
[html]  view plain  copy
  1. <TextView   
  2.     style="@style/TextViewStyle1"  
  3.     android:layout_marginTop="100dp"  
  4.     android:text="test1"/>  
  5.    
  6. <TextView   
  7.     style="@style/TextViewStyle1"  
  8.      android:layout_marginTop="200dp"  
  9.     android:text="test2"/>  
  10. <TextView   
  11.     style="@style/TextViewStyle1"  
  12.      android:layout_marginTop="300dp"  
  13.     android:text="test3"/>  
  14. <TextView   
  15.     style="@style/TextViewStyle1"  
  16.      android:layout_marginTop="400dp"  
  17.     android:text="test4"/>  
[html]  view plain  copy
  1. 那么结果就是<img src="https://img-blog.csdn.net/20140913101147703?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGV3ZW5jZTE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />  
[html]  view plain  copy
  1.   
[html]  view plain  copy
  1. 那么我们在<TextView 中使用了Style 而且使用了与style中相冲突会早呢么样呢?  
[html]  view plain  copy
  1. 修改第一个textView的背景跟颜色:  
[html]  view plain  copy
  1. <pre name="code" class="html"><TextView   
  2.       style="@style/TextViewStyle1"  
  3.       android:layout_marginTop="100dp"  
  4.       android:gravity="center_horizontal"  
  5.       android:background="#ff00ff00"  
  6.       android:textColor="#ffffffff"  
  7.       android:text="test1"/>  
那么结果就是

 
[html]  view plain  copy
  1. 由此可以见,相关view的属性包括style中的所有的属性,view中自己还定义了的就使用view字定义的  
[html]  view plain  copy
  1. style中的属性,在view中没有作用的会自动忽略掉  
[html]  view plain  copy
  1.   
2.style的继承

    1.加上parent

<style name="TextViewStyle2" parent="@style/TextViewStyle1">        <item name="android:layout_width">400dp</item>    </style>

2.加点

    <style name="TextViewStyle1.test">        <item name="android:layout_width">800dp</item>    </style>

  还可以多继承:

<style name="TextViewStyle1.test.test">        <item name="android:layout_width">1200dp</item>    </style>

那么布局文件改成:

  

[html]  view plain  copy
  1. <TextView   
  2.       style="@style/TextViewStyle1"  
  3.       android:layout_marginTop="100dp"  
  4.       android:gravity="center_horizontal"  
  5.       android:background="#ff00ff00"  
  6.       android:textColor="#ffffffff"  
  7.       android:text="test1"/>  
  8.      
  9.   <TextView   
  10.       style="@style/TextViewStyle1.test.test"  
  11.        android:layout_marginTop="200dp"  
  12.       android:text="test2"/>  
  13.   <TextView   
  14.       style="@style/TextViewStyle2"  
  15.        android:layout_marginTop="300dp"  
  16.       android:text="test3"/>  
  17.   <TextView   
  18.       style="@style/TextViewStyle1.test"  
  19.       android:layout_marginTop="400dp"  
  20.       android:text="test4"/>  
输出结果如下:

sytle的更多属性见android包下的R.attr,这些都是系统的哦!

使用Theme,这个就猛了,改了以后会影响真个程序的显示:

系统默认有:

[html]  view plain  copy
  1. <application  
  2.         android:allowBackup="true"  
  3.         android:icon="@drawable/ic_launcher"  
  4.         android:label="@string/app_name"  
  5.         android:theme="@style/AppTheme" >  
我先把
[html]  view plain  copy
  1. AppTheme修改一下吧:  
[html]  view plain  copy
  1. 加入二个元素:<pre name="code" class="html"> <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">  
  2.         <!-- API 14 theme customizations can go here. -->  
  3.         <strong><item name="android:textSize">60sp</item>  
  4.         <item name="android:typeface">monospace</item></strong>  
  5.     </style>  
那么系统所有的文字的大少都是60sp,字体使用monospace(除非你们的View重新定义了)

 
更多的系统style可以在: 

sdk\platforms\andrid-$API\data\res\themes.xm   styles.xml

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值