Android的Style的使用

简介:   Style个人理解就是view的一些属性的集合,那么一系列view(例如TextVIew),只要是要该style那么就都有相同的内容,如 文字的大少,颜色等,方便修改首先最基本的使用,多个textView都显示一样的颜色 跟文字大少等属性   Sytle的定义:[java] view plaincopyprint?<style  name="TextViewSty

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

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

   Sytle的定义:

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

 
  1. 由此可以见,相关view的属性包括style中的所有的属性,view中自己还定义了的就使用view字定义的  
由此可以见,相关view的属性包括style中的所有的属性,view中自己还定义了的就使用view字定义的
  1. style中的属性,在view中没有作用的会自动忽略掉  
style中的属性,在view中没有作用的会自动忽略掉
  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>

那么布局文件改成:

  

  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"/>  
<TextView 
      style="@style/TextViewStyle1"
      android:layout_marginTop="100dp"
      android:gravity="center_horizontal"
      android:background="#ff00ff00"
      android:textColor="#ffffffff"
      android:text="test1"/>
   
  <TextView 
      style="@style/TextViewStyle1.test.test"
       android:layout_marginTop="200dp"
      android:text="test2"/>
  <TextView 
      style="@style/TextViewStyle2"
       android:layout_marginTop="300dp"
      android:text="test3"/>
  <TextView 
      style="@style/TextViewStyle1.test"
      android:layout_marginTop="400dp"
      android:text="test4"/>
输出结果如下:


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

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

系统默认有:

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

更多的系统style可以在: 

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

目录
相关文章
|
9月前
|
API Android开发 UED
|
API Android开发
Android中的Style、Theme详解已经发展史
版权声明:本文为sydMobile原创文章,转载请务必注明出处! https://blog.csdn.net/sydMobile/article/details/80164916 Style介绍 style就像单词意思一样,风格,这里面是属性的集合,如果页面中有许多控件的属性值相同那么就可以把这些属性抽出来放到style里面,定义也很简单,在values文件下的styles里面创建就可以了。
1631 0
|
Android开发 Java 开发工具