生活随笔
收集整理的這篇文章主要介紹了
Android的Style的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Style個人理解就是view的一些屬性的集合,那么一系列view(例如TextVIew),只要是要該style那么就都有相同的內容,如 文字的大少,顏色等,方便修改
首先最基本的使用,多個textView都顯示一樣的顏色 跟文字大少等屬性
? ?Sytle的定義:
[java]?view plain
?copy <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>?? 這些屬性都熟悉,使用
[html]?view plain
?copy <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"/>?? [html]?view plain
?copy 那么結果就是<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 ?? [html]?view plain
?copy 那么我們在<TextView?中使用了Style?而且使用了與style中相沖突會早呢么樣呢??? [html]?view plain
?copy 修改第一個textView的背景跟顏色:?? [html]?view plain
?copy <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"/>?? 那么結果就是
[html]?view plain
?copy 由此可以見,相關view的屬性包括style中的所有的屬性,view中自己還定義了的就使用view字定義的?? [html]?view plain
?copy style中的屬性,在view中沒有作用的會自動忽略掉?? [html]?view plain
?copy ?? 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 <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,這個就猛了,改了以后會影響真個程序的顯示:
系統默認有:
?
[html]?view plain
?copy <application??????????android:allowBackup="true"?? ????????android:icon="@drawable/ic_launcher"?? ????????android:label="@string/app_name"?? ????????android:theme="@style/AppTheme"?>?? 我先把
[html]?view plain
?copy AppTheme修改一下吧:?? [html]?view plain
?copy 加入二個元素:<pre?name="code"?class="html">?<style?name="AppBaseTheme"?parent="android:Theme.Holo.Light.DarkActionBar">?? ???????? ????????<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
?
2.自定義屬性的使用
其實我們在style.xml中使用自定義屬性的話,不需要寫自定義控件的命名空間,我們只需要在style中使用命名控件的地方換成自定義控件的包名即可(注意:是包名,不帶自定義控件的名字),如下:
[html]?view plain
?copy? <resources?xmlns:android="http://schemas.android.com/apk/res/android"?>??????<style?name="test"?>?? ???? <item?name="android:textSize">60sp</item>?
????????<item?name="com.zhufuing:name_text">hello,world!</item>????????? ????</style>?? </resources>??
?
? ? 本文轉自 一點點征服 ? 博客園博客,原文鏈接:http://www.cnblogs.com/ldq2016/p/5226528.html,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的Android的Style的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。