Android常用的几种资源
2019獨角獸企業(yè)重金招聘Python工程師標準>>>
Android常用的幾種資源
一般資源都是放在res文件夾下的,res文件夾一般包含有values,drawable-ldpi、drawabel-mdpi、drawabel-hdpi和layout等子文件夾;drawable資源文件夾使用3種不同的圖標,分別對應(yīng)低、中、高三種密度的顯示屏。
Values文件夾下一般放一些簡單值。Android支持的簡單值一般包括 字符串、顏色、尺寸和字符串數(shù)組或者整形數(shù)組。他們都放在一個xml文件里。
字符串:<string name="hello">Hello World</string>
Android還支持簡單的文本樣式,所以可以使用HTML標簽<b><i><u>來讓文本字符串變?yōu)榇煮w、斜體活帶有下滑線。
Example:<string name="hello"> <b>Hello World <b> </string>
但為String.format方法輸入?yún)?shù)時可以使用字符串。但是String.format不支持上面描述的文本樣式。為了對一個格式化字符串使用樣式時,需要在創(chuàng)建資源的時候轉(zhuǎn)義HTML標簽:
Example:<string name="hello"> < b > Hello World < b > </string>
在代碼內(nèi),可以使用Html.fromHtml方法把這些字符串轉(zhuǎn)換回樣式字符串序列;
String rString = getString(R.string.hello);
String fString = String.format(rString,”Collaborate and listen”);
CharSequence styledString = Html.fromHtml(fString);
還可以定義String 數(shù)組:
Example:<string name="hello"> < b > Hello World < b > </string>
還可以為字符串定義復數(shù)形式。這個適合外語,比如英語里面: 一首歌:one song ,超過一首的要使用復數(shù):two songs ,這是就要用到復數(shù)資源<plurals>
Example:<plurals name="songCount">
??? ??? ?????? <item quantity="one">one song</item>
??? ??? ?????? <item quantity="other">%d songs</item>???????
??? ??? ?</plurals>
在代碼中:
int count =3; //要表示的數(shù)量
String songcount =
getResources().getQuantityString(R.plurals.songCount, count, count);//計數(shù)要傳入兩次,一次是返回正確的復數(shù)字符串,一次是作為輸出的
得到的結(jié)果 songcount = 3 songs;
關(guān)于<plurals>跟詳細參看:http://developer.android.com/guide/topics/resources/string-resource.html#Plurals
顏色:使用color標簽定義一種的新的顏色資源。使用#符號來指定顏色值。其后跟一個可選的Alpha通道,在之后使用一個或者兩個十六進制值表示紅、綠、藍 值;
Example:
#RGB
#RRGGBB
#ARGB
#AARRGGBB
?
尺寸:可以使用<dimen> 標簽來指定尺寸的值。
尺寸單位:
px(屏幕像素)
in(物理英寸)
pt(物理點)
mm(物理毫米)
dp(非密度制約的像素)(建議使用)
樣式 和 主題
要建一個樣式使用style標簽,包含一個name屬性一個或多個item標簽。每一個item 標簽都應(yīng)該包含一個name屬性來指定要定義的屬性,然后標簽本身應(yīng)該包含值。
?<resources>
??? <style name="CustomText">
??????? <item name="android:textSize">20sp</item>
??????? <item name="android:textColor">#008</item>
??? </style>
</resources>
樣式支持通過style標簽上使用parent 屬性 來進行繼承,從而使創(chuàng)建簡單的變體形式變得簡單。
?<resources>
??? <style name="CustomText" parent="@style/Text">
??????? <item name="android:textSize">20sp</item>
??????? <item name="android:textColor">#008</item>
??? </style>
</resources>
?
關(guān)于其它資源drawable、布局、動畫等都挺簡單,若要了解請查看
http://developer.android.com/guide/topics/resources/available-resources.html
轉(zhuǎn)載于:https://my.oschina.net/litengit/blog/354286
總結(jié)
以上是生活随笔為你收集整理的Android常用的几种资源的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Handlebars模板库浅析
- 下一篇: android sina oauth2.