android各種資源的詳細解釋

來源:互聯網
上載者:User

標籤:

1、字元數組
     使用字串數組資源<string-array>標籤定義,在<string-array>包括一些標籤<item>數組元素標記。
  例如
<?

xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array  name="planets_array">
        <item>Mercury</item>
        <item>Values</item>
        <item>Earth</item>
        <item>Mars</item>
    </string-array>
</resources>

在代碼中引用字串數組的代碼例如以下:
String[] plans=getResources().getStringArray(R.array.planets_array);

注意:不能用字串資料資源設定接收字串的屬性,比如,android:text,由於這樣系統會直接將字串數組資源的ID值
當做文本傳給該屬性,應該用字串資源設定那些能夠接收字串數組資源的屬性。

2、複數字串

<?

xml version="1.0" encoding="utf-8"?

>

<resources>

<plurals

name="plural_name">

<item_plurals

quantity=["zero" | "one" | "two" | "few" | "many" | "other"]

>text_string</item>

</plurals>

</resources>

 

            zero           語言須要對數字0進行特殊處理。(比方阿拉伯語)
 

            one      語言須要對類似1的數字進行特殊處理。

(比方英語和其他大多數語言裡的1;在俄語裡,不論什麼以1結尾但不以11結尾的數也屬於此類型。)
 

            two           語言須要對類似2的數字進行特殊處理。(比方威爾士語)
 

            few            語言須要對較小數字進行特殊處理(比方捷克語裡的2、3、4;或者波蘭語裡以2、3、4結尾但不是12、13、14的數。)
 

            many      語言須要對較大數字進行特殊處理(比方馬爾他語裡以11-99結尾的數)
 

            other          語言不須要對數字進行特殊處理。

 使用複數字串資源能夠使用getQuantityString方法。該方法有兩個重載形式。它們的原型例如以下:
 public String getQuantityString(int id, int quantity) throws NotFoundException;
 public String getQuantityString(int id, int quantity, Object... formatArgs)throws NotFoundException; 

當中id參數表示複數字串資源的ID,quantity參數表示詳細的數字,比如,1相應quantity屬性值的"one"。2相應quantity屬性值的"two"。formatArgs參數表示複數字串資源的參數。
 
 //  引用數字為1的複數字串資源  setTitle(getResources().getQuantityString(               
 R.plurals.numberOfSongsAvailable, 1)); 
 //  引用數字為other的複數字串資源,調用時向other資源傳遞一個參數值(20)
 setTitle(getResources().getQuantityString(              
 R.plurals.numberOfSongsAvailable, 20, 20)); 

 
 3、在字串中使用引號
     字串中的值儘管能夠隨意指定,但遇到特殊符號時。如雙引號、單引號,就須要採取特殊的方法進行處理
  假設是(‘),能夠使用轉義符(\)或使用雙引號(")將整個字串括起來。假設是雙引號,能夠在雙引號前使用轉義符(\)。比如:
  <!-- 輸出This‘ll work -->
    <string name="str1">"This‘ll work"</string>
    <!--輸出This ‘ll also work  -->
    <string name="str2">This \‘ll also work</string>
    <!-- 輸出"apple" -->
    <string name="str3">\"apple\"</string>
 
 4、用預留位置格式化字串 
    String.format(String,Object..)方法能夠格式化帶預留位置的字串。

因此。僅僅要在字串資源中插入預留位置就能夠
 是用String.format方法格式化字串資源。

format方法要求預留位置用%1、%2、。

。。%n表示。

當中第n個預留位置於format
 方法的n+1個參數值相應。

 帶預留位置的字串資源
 <!-- $s 表示該預留位置要求傳入字串, $d 表示該預留位置要求傳入整數  %後面的整數表示第幾個預留位置-->
    <string name="welcome">hello, %1$s!

youhanv %2$d/%3$d new messages</string>
 
 格式化字串資源的java代碼
 String textString=String.format(getResources().getString(R.string.welcome), "hello",18,20);

  5、用HTML標籤格式化字串資源
     字串資源支援一些HTML標籤。因此。能夠直接在字串資源中使用這些HTML標籤格式化字串。
  用HTMl標籤格式化的字串資源
  <string name="welcome">Welcome to <b>Android<b/></string>
 
 字串資源支援例如以下的HTML標籤
 <b>:粗體字
 <i>:斜體字
 <u>:帶底線的文字
 有時須要同一時候使用HTML標籤和預留位置格式化字串,但使用String.format方法格式字串,會忽略字串中全部的HTML標籤。為了是
 format方法能夠格式化帶HTMl標籤的字元,須要使用Html.fromHTML方法先處理一下字串。比如
 <string name="welcome">hello, %1$s!

You have $lt; <b> %2d new messages$lt;</b></string>
 
 注意:因為須要使用Html.formatHTML方法處理字串,因此,HTMl標籤中的"<"須要使用"&lt;" 表示(">"能夠直接使用)
 使用字串資源的Java代碼
 String textString=String.format(getResources().getString(R.string.welcome), "hello",18,20);
    CharSequence styledText=Html.fromHtml(text);
 假設format 的某個參數值包括HTML的特殊字元串,如"<","&"。能夠使用以下的代碼先格式化這個參數值,在使用format方法格式化字串
 //username中包括HTML的特殊字元
 String escapedUsername=TextUtil.htmlEncode()username;
 String textString=String.format(getResources().getString(R.string.welcome),escapedUsername,malCount);
    CharSequence styledText=Html.fromHtml(text);

  6、值資源
     全部放到res/values檔案夾中的資源都屬於值資源(能夠存放在隨意的XML檔案裡)。
  1、整數資源
    整數資源使用<integer>標籤設定。代碼例如以下:
    <integer name="height">75</integer>
    <integer name="width">75</integer>
    在資源檔裡使用的代碼
    <TextView
        android:id="@+id/text"
        android:layout_width="@integer/height"
        android:layout_height="@integer/width"
        android:layout_alignParentBottom="true"
        android:text="@string/str3"
        />
  以下是引用整數資源的java代碼
  int width=getResources.getInteger(R.integer.width);
 2、尺寸資源
    尺寸資源是由一系列的浮點數組成的資源,這些資源須要在res/values檔案夾的資源檔裡定義,<dimen>標籤用來定義尺寸資源。


    <dimen name="size_px">50px</dimen>
       <dimen name="size_in">1.5in</dimen>
       <dimen name="size_sp">10sp</dimen>
    使用java代碼擷取尺寸資源,這些方法會依據相應的尺寸單位返回與其相應的像素值
    float size1=getResources().getDimension(R.dimen.size_in);
   
    假設想直接擷取尺寸大小(不轉化成像素),能夠使用以下的代碼
    TypedValue outValue=new TypedValue();
  //getValue方法的第3個參數假設為true,表示即使資源值引用了另外的資源,系統會通過遞迴的方法擷取終於的資源
  getResources().getValue(R.dimen.size_px, outValue, true);
  //因為getValue方法並沒有直接返回尺寸資源的值,所以須要使用complexToFloat方法進行轉換
  float value=TypedValue.complexToFloat(outValue.data);
 3、顏色資源
    顏色資源用於指定顏色值,使用<color>標籤設定,代碼例如以下
    <color name="red">#f00</color>
    java代碼獲代替碼
    int color=getResources().getColor(R.color.red);
 4、ID資源
    ID資源實際上就是android:id屬性的值,使用<item>標籤設定。代碼例如以下:
    <item type="id" name="button_ok"></item>
       <item type="id" name="dialog_exit"></item>
    資源檔裡引用ID資源的代碼例如以下:
    <Button android:id="@id/button_ok"/>
    通常在設定android:id屬性時都會在“@”和“id”之間加一個“+”,代碼例如以下:
    <Button android:id="@+id/button_ok"/>
    這個“+”的含義是假設ID資源不存在,系統會自己主動建立一個ID資源。假設ID資源存在,系統會忽略“+”。


    通過這樣的策略避免在設定android:id屬性值時都要定義ID資源的繁瑣方式。


    注意:雖然全部可接受ID資源的屬性都能夠使用“+”,但建議僅僅在android:id屬性值使用“+".這是由於其它的屬性須要使用已經存在的ID
    資源相應的控制項,假設使用”+“。當指定的ID資源不存在是系統會自己主動建立一個ID資源,這樣雖然ID資源存在了,但並未相應不論什麼的控制項。可能
    會使程式拋出異常,也不會是程式達到預先設定的效果。比如,<RelativeLayout>標籤的android:layout_marginLeft屬性就必須設定一個
    已經與某個控制項綁定的ID資源。


   
 5、整數數組資源
    整數數組資源用於儲存一組整數。用<integer-array>標籤設定,代碼例如以下:
    <integer-array name="bits">
         <item>4</item>
         <item>5</item>
         <item>6</item>
         <item>7</item>
       </integer-array>
    引用整數數組資源的java代碼例如以下:
    int[] bits=getResources().getIntArray(R.array.bits);
   
 6、類型數組資源
    類型數組資源能夠將當中資源作為數組儲存,因此也能夠稱為資源數組資源。
    <array name="icons">
         <item>@drawable/home</item>
    </array>
    <array name="colos">
         <item>#ffff0000</item>
    </array>
   
    參考型別數組資源的java代碼例如以下:
    TypeArray drawable=getResources().obtainTypedArray(R.array.icons);
    Drawable drawable=icons.getDrawable(0);
   
    TypeArray color=getResources().obtainTypedArray(R.array.colos);
    int color=color.getColor(0,0)
    
    
    
    
 
 

著作權聲明:本文部落格原創文章,部落格,未經同意,不得轉載。

android各種資源的詳細解釋

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.