Android <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类的相应子类中生成变量;

 

posted @ 2013-06-16 21:44  Livingstone  阅读(3880)  评论(0编辑  收藏  举报