style个人理解就是view的一些属性的集合,那么一系列view(例如textview),只要是要该style那么就都有相同的内容,如 文字的大少,颜色等,方便修改
首先最基本的使用,多个textview都显示一样的颜色 跟文字大少等属性
sytle的定义:
<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>
这些属性都熟悉,使用
<textview
style="@style/textviewstyle1"
android:layout_margintop="100dp"
android:text="test1"/>
android:layout_margintop="200dp"
android:text="test2"/>
android:layout_margintop="300dp"
android:text="test3"/>
android:layout_margintop="400dp"
android:text="test4"/>
那么结果就是<img src="http://img.blog.csdn.net/20140913101147703?watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvagv3zw5jzte=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/center" alt="" />
那么我们在<textview 中使用了style 而且使用了与style中相冲突会早呢么样呢?
修改第一个textview的背景跟颜色:
<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"/>
那么结果就是

由此可以见,相关view的属性包括style中的所有的属性,view中自己还定义了的就使用view字定义的
style中的属性,在view中没有作用的会自动忽略掉
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>
那么布局文件改成:
<textview
style="@style/textviewstyle1.test.test"
android:layout_margintop="200dp"
android:text="test2"/>
style="@style/textviewstyle2"
android:layout_margintop="300dp"
android:text="test3"/>
style="@style/textviewstyle1.test"
android:layout_margintop="400dp"
android:text="test4"/>
输出结果如下:
sytle的更多属性见android包下的r.attr,这些都是系统的哦!
使用theme,这个就猛了,改了以后会影响真个程序的显示:
系统默认有:
<application
android:allowbackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/apptheme" >
我先把
apptheme修改一下吧:
加入二个元素:<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