代码改变世界

使用字符串、颜色、尺寸资源

2013-11-22 09:39  Andy Ge  阅读(483)  评论(0编辑  收藏  举报
字符串资源、颜色资源、尺寸资源,它们对应的 XML 文件都将位于/res/values 目录下, 它们默认的文件名,以及在 R 类中对应的内部类如下所示:
 
 
不过其中的默认文件名称是可以修改的。
示例代码如下:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    >
<!-- 使用字符串资源,尺度资源 -->
<TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/app_name"
    android:gravity="center"
    android:textSize="@dimen/title_font_size"
/>
<!-- 定义一个GridView组件,使用尺度资源中定义的长度来指定水平间距、垂直间距 -->
<GridView  
    android:id="@+id/grid01"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:horizontalSpacing="@dimen/spacing"
    android:verticalSpacing="@dimen/spacing"
    android:numColumns="3"
    android:gravity="center">
</GridView>
</LinearLayout>
资源文件的名称是可以修改,当然也可以定义多个相同类型的资源文件,但注意所有的资源文件都是以<resources>为根目录的,至于里面的类型则为对应的color,demen,integer,string,boolean等,这些标签是不能更改的,编译器根据这些来生成R.java中的类。
color3.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="c1">#F00</color>
    <color name="c2">#0F0</color>
    <color name="c3">#00F</color>
    <color name="c4">#0FF</color>
    <color name="c5">#F0F</color>
    <color name="c6">#FF0</color>
    <color name="c7">#07F</color>
    <color name="c8">#70F</color>
    <color name="c9">#F70</color>
</resources>
dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="spacing">8dp</dimen>
    <!-- 定义GridView组件中每个单元格的宽度、高度 -->
    <dimen name="cell_width">60dp</dimen>
    <dimen name="cell_height">66dp</dimen>
    <!-- 定义主程序的标题的字体大小 -->
    <dimen name="title_font_size">18sp</dimen>
</resources>
integers.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="font_size">20</integer>
</resources>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, ValuesResTest!</string>
    <string name="app_name">字符串、数字、尺寸资源</string>
    <string name="c1">F00</string>
    <string name="c2">0F0</string>
    <string name="c3">00F</string>
    <string name="c4">0FF</string>
    <string name="c5">F0F</string>
    <string name="c6">FF0</string>
    <string name="c7">07F</string>
    <string name="c8">70F</string>
    <string name="c9">F70</string>    
</resources>
ValueResTest.java
package org.crazyit.res;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

/**
 * Description:
 * <br/>site: <a href="http://www.crazyit.org">crazyit.org</a>
 * <br/>Copyright (C), 2001-2014, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class ValuesResTest extends Activity
{
    // 使用字符串资源
    int[] textIds = new int[]
    {
        R.string.c1 , R.string.c2 , R.string.c3 ,
        R.string.c4 , R.string.c5 , R.string.c6 ,
        R.string.c7 , R.string.c8 , R.string.c9
    };
    // 使用颜色资源
    int[] colorIds = new int[]
    {
        R.color.c1 , R.color.c2 , R.color.c3 ,
        R.color.c4 , R.color.c5 , R.color.c6 ,
        R.color.c7 , R.color.c8 , R.color.c9
    };
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // 创建一个BaseAdapter对象
        BaseAdapter ba = new BaseAdapter()
        {
            @Override
            public int getCount()
            {
                // 指定一共包含9个选项
                return textIds.length;
            }

            @Override
            public Object getItem(int position)
            {
                // 返回指定位置的文本
                return getResources().getText(textIds[position]);
            }

            @Override
            public long getItemId(int position)
            {
                return position;
            }
            // 重写该方法,该方法返回的View将作为的GridView的每个格子
            @Override
            public View getView(int position
                , View convertView, ViewGroup parent)
            {
                TextView text = new TextView(ValuesResTest.this);
                Resources res = ValuesResTest.this.getResources();
                // 使用尺度资源来设置文本框的高度、宽度
                text.setWidth((int) res.getDimension(R.dimen.cell_width));
                text.setHeight((int) res.getDimension(R.dimen.cell_height));
                // 使用字符串资源设置文本框的内容
                text.setText(textIds[position]);
                // 使用颜色资源来设置文本框的背景色
                text.setBackgroundResource(colorIds[position]);
                text.setTextSize(20);
                text.setTextSize(getResources()
                    .getInteger(R.integer.font_size));
                return text;
            }
        };
        GridView grid = (GridView)findViewById(R.id.grid01);
        // 为GridView设置Adapter
        grid.setAdapter(ba);
    }
}
编译器自动生成的R.java文件:
/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package org.crazyit.res;

public final class R {
    public static final class attr {
    }
    public static final class color {
        public static final int c1=0x7f040000;
        public static final int c2=0x7f040001;
        public static final int c3=0x7f040002;
        public static final int c4=0x7f040003;
        public static final int c5=0x7f040004;
        public static final int c6=0x7f040005;
        public static final int c7=0x7f040006;
        public static final int c8=0x7f040007;
        public static final int c9=0x7f040008;
    }
    public static final class dimen {
        public static final int cell_height=0x7f050002;
        /**  定义GridView组件中每个单元格的宽度、高度
         */
        public static final int cell_width=0x7f050001;
        public static final int spacing=0x7f050000;
        /**  定义主程序的标题的字体大小
         */
        public static final int title_font_size=0x7f050003;
    }
    public static final class drawable {
        public static final int ic_action_search=0x7f020000;
        public static final int ic_launcher=0x7f020001;
    }
    public static final class id {
        public static final int grid01=0x7f080000;
    }
    public static final class integer {
        public static final int font_size=0x7f060000;
    }
    public static final class layout {
        public static final int main=0x7f030000;
    }
    public static final class string {
        public static final int app_name=0x7f070001;
        public static final int c1=0x7f070002;
        public static final int c2=0x7f070003;
        public static final int c3=0x7f070004;
        public static final int c4=0x7f070005;
        public static final int c5=0x7f070006;
        public static final int c6=0x7f070007;
        public static final int c7=0x7f070008;
        public static final int c8=0x7f070009;
        public static final int c9=0x7f07000a;
        public static final int hello=0x7f070000;
    }
}
及BuildConfig.java:
/** Automatically generated file. DO NOT MODIFY */
package org.crazyit.res;

public final class BuildConfig {
    public final static boolean DEBUG = true;
}
屏幕截图: