Android 資源類型<二>


1.菜單資源
菜單不僅可以在onCreateContextMenu或onCreateOptionsMenu方法中通過代碼創建,還可以在res/menu目錄中建立相應的菜單資源文件,並在上面兩個方法中加載菜單資源;
菜單資源文件必須以<menu>標簽作為根節點,每一個菜單項用一個<item>表示,如果要定義子菜單,可以在<item>標簽中包含<menu>標簽;如果想將多個菜單項划為一組,可以使用<group>包含多個<item>標簽;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return true;
    }

查看MenuInflater.inflate(int,Menu)

    /**
     * Inflate a menu hierarchy from the specified XML resource. 
     * 
     * @param menuRes Resource ID for an XML layout resource to load (e.g., <code>R.menu.main_activity</code>)
     * @param menu The Menu to inflate into. The items and submenus will be added to this Menu.
     */
    public void inflate(int menuRes, Menu menu) {
        XmlResourceParser parser = null;
        try {
            parser = mContext.getResources().getLayout(menuRes);
            AttributeSet attrs = Xml.asAttributeSet(parser);
            
            parseMenu(parser, attrs, menu);
        } catch ...finally {
            if (parser != null) parser.close();
        }
    }

2.樣式與主題(style/theme)
>>1.樣式style
android中樣式和css中樣式作用是一樣的,都是用於為界面元素定義顯示風格,它是一個包含一個或者多個控件屬性的集合。
定義樣式需要在res/values/styles.xml中進行定義,如下是一個樣式的定義:

<style name="textViewStyle">
  <item name="android:textSize">22sp</item>
  <item name="android:textColor">#FF0000</item>
</style>
<style name="textViewStyle1" parent="textViewStyle"></style><!-- 此樣式繼承自textViewStyle -->
<style name="textViewStyle.Livingstone"><!-- 樣式繼承的另一種寫法,但不可用此寫法繼承Android自帶的定義樣式? -->
  <item name="android:textColor">#00FF00</item>
</style>

所有定義的樣式都會在R文件中自動生成一個資源ID,加一個點表示樣式繼承會生成上圖所示的資源id;

樣式的引用:

<TextView
  style="@style/textViewStyle"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="hello" />

>>2.主題Theme
主題應用於整個應用或者activity,樣式應用於具體的控件上。主題的應用與樣式定義一樣,不同的是主題還可以設置窗口的顯示風格;主題的引用需要在清單文件中進行引用,如引用到整個應用之上就需要在Application節點中進行配置引用,而引用到單個Activity只需要在此Activity中進行配置引用;

<style name="Livingstonetheme"><!--此定義是一個無Title的主題-->
  <item name="android:windowNoTitle">true</item>
  <item name="android:windowFullscreen">?android:windowNoTitle</item>
  <!-- 問號表示引用此主題中android:windowNoTitle屬性的值 -->
  <item name="android:textSize">18sp</item>
</style>

android系統定義了一些屬性,如android:theme="@android:style/Theme.Dialog",該主題可以讓Activity看起來像一個對話框,更多主題可以在文檔reference->android->R.style中查看。當主題里面的樣式屬性值與樣式里面的屬性值發生沖突的時候會顯示樣式里面的值;

3.xml資源

在android中,除上上述的資源之外,還可以直接讀取xml資源;
例子:

res/xml/users.xml

<?xml version="1.0" encoding="utf-8"?>
<users>
    <user
        name="liming"
        age="23" />
    <user
        name="zhangsan"
        age="25" />
</users>
        Resources res = getResources();
        XmlResourceParser p = res.getXml(R.xml.users);
        try {
            while (p.getEventType() != XmlResourceParser.END_DOCUMENT) {
                if (p.getEventType() == XmlResourceParser.START_TAG) {
                    if (p.getName().equals("user")) {
                        Log.e("tag", "name:" + p.getAttributeValue(null, "name"));
                        Log.e("tag", "age:" + p.getAttributeValue(null, "age"));
                    }
                }
                p.next();
            }
        } catch (XmlPullParserException e) {
        } catch (IOException e) {
        }

 

4.其它資源
在資源文件中還可以包括尺寸(dimen)、整數(integer)、布爾(bool) 、整形數組資源(integer-array)、資源數組(array)、顏色(color)

TypedArray ta = getResources().obtainTypedArray(int id); // 獲取數組資源,包括integer-array、array

Final總結:
除了res/values目錄中的資源名,其它目錄的資源都會以文件名在R類的相應子類中生成變量;而res/values中的資源會以name屬性值為變量名在R類的相應子類中生成變量;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM