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