資源Resource之values

常用資源文件

這裏寫圖片描述

1、字符串資源文件:strings

<resources>
            <string name="app">應用程序</string>
            <string name="helloword">Hello World!</string>
        </resources>
    Java文件中獲取方式:getResources().getString(R.string.red);

2、顏色資源文件:colors

     <resources>
            <color name="red">#f00</color>
            <color name="green">#0f0</color>
         </resources>
    Java文件中獲取方式:getResources().getColor(R.color.blue);

3、尺寸資源文件:dimens

     <resources>
            <dimen  name="title">20dp</dimen>
            <dimen  name="content">10dp</dimen>
         </resources>
    Java文件中獲取方式getResources().getDimension(R.dimen.title);

4、數組資源文件:arrays

    Android中,不推薦將數組直接定義在java文件中,而是使用數組資源文件來定義數組。
    根元素:同上<resources></resources>
    子元素:可以有三種。
                 <array></array>:稱爲類型數組Typed-Array資源(或者:資源數組資源)
                 <string-array></string-array>:字符串數組資源
                 <integer-array></integer-array>:整形數組資源
    下級子元素:<item>數值</item>
 <resources>
            <string-array name="choiceItems"> 
                <item >主屏模式</item>
                <item >wifi設置</item>
                <item >藍牙設置</item>
                <item >網絡設置</item>
            </string-array>
         </resources>
     Java文件中獲取方式:
  getResources().getStringArray(R.array.choiceItems);
  <resources>
            <array name="icons"> 
                <item >@drawable/home</item>
                <item >@drawable/setting</item>
                <item >@drawable/logout</item>
            </array>
            <array name="title"> 
                <item >首頁</item>
                <item >設置</item>
                <item >退出</item>
            </array>
        </resources>
    Java文件中獲取方式:
    TypedArray  arrIcons =getResources().obtainTypedArray(R.array.icons);                                              Drawable drawable = arrIcons.getDrawable(0);                                             arrIcons.recycle();

5、樣式/主題資源文件:styles

  • 樣式
    將一些常用的屬性組合成樣式,便於重複使用,減少給View控件指定類似屬性的重複工作。Android Style類似網頁設計中的級聯樣式CSS設計思路,可以讓設計與內容分離,並且可以方便的繼承、覆蓋、重用。
<color name="Black">#000000</color>
<style name="popup_baseStyle">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_gravity">center</item>
        <item name="android:textColor">@color/Black</item>
  </style>
  //1.通過parent屬性繼承
<style name="popup_box_tv" parent="popup_baseStyle">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_weight">1</item>
 </style>
 //2.通過style命名規則繼承
  <style name="popup_baseStyle.A">
  <item name="android:layout_width">0dp</item>
  </style>
在佈局的View中使用樣式:
<TextView style = "@style/popup_box_tv"
  • 主題
    在定義上樣式和主題基本相同,但使用的地方不同。兩者區別在於:
    1. 主題通過AndroidManifest.xml中的和用在整個應用或者某個 Activity,主題對整個應用或某個Activity存在全局性影響。而樣式都寫在Activity佈局中,用在單獨的View如TextView;
    2. 主題定義的格式應該是改變窗口外觀的格式:例如窗口標題、窗口邊框等;
    3. 如果一個應用使用了主題應用下的view也使用了樣式,當主題與樣式屬性發生衝突時,樣式的優先級高於主題。(也就是誰最靠近UI控件,誰起作用)
 <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowNoTitle">true</item>
    </style>
在AndroidManifest.xml中引用主題:
<activity android:theme="@style/CustomTheme">

6、自定義屬性文件:attrs

    <declare-styleable name="ImageButtonWithText">
        <attr name="text_img" format="string"/>
        <attr name="textSize_img" format="dimension"/>
        <attr name="textColor_img" format="color"/>
    </declare-styleable>
Java文件中獲取方式:
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ImageButtonWithText, 0, defStyle);
string s =a.getText(R.styleable.ImageButtonWithText_text_img);
a.recycle();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章