下面是使用字符串,数字,尺寸,数组资源的例子,挺好,一个例子已经囊括了不少Android资源的使用方法
arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name = "plain_arr">
<item>@color/c1</item>
<item>@color/c2</item>
<item>@color/c3</item>
<item>@color/c4</item>
<item>@color/c5</item>
<item>@color/c6</item>
<item>@color/c7</item>
<item>@color/c8</item>
<item>@color/c9</item>
</array>
<string-array name = "string_arr">
<item >@string/c1</item>
<item >@string/c2</item>
<item >@string/c3</item>
<item >@string/c4</item>
<item >@string/c5</item>
<item >@string/c6</item>
<item >@string/c7</item>
<item >@string/c8</item>
<item >@string/c9</item>
</string-array>
<string-array name = "books">
<item >Java</item>
<item >Ajax</item>
<item >Android</item>
</string-array>
</resources>
colors.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>
<dimen name="cell_width">60dp</dimen>
<dimen name="cell_height">66dp</dimen>
<dimen name="title_font_size">18sp</dimen>
</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>
主界面xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http:///apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<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
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>
<ListView
android:id="@+id/listView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/books"
>
</ListView>
</LinearLayout>
代码
package WangLi.Resources.Values;
import android.app.Activity;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;
public class ValuesResTest extends Activity {
/** Called when the activity is first created. */
String[] texts;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
texts = getResources().getStringArray(R.array.string_arr);
//创建一个BaseAdapter对象
BaseAdapter ba = new BaseAdapter()
{
public int getCount()
{
return texts.length;
}
public Object getItem(int position)
{
return texts[position];
}
public long getItemId(int position)
{
return position;
}
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(texts[position]);
TypedArray icons = res.obtainTypedArray(R.array.plain_arr);
//使用颜色资源来设置文本框的背景色
text.setBackgroundDrawable(icons.getDrawable(position));
text.setTextSize(20);
return text;
}
};
GridView grid = (GridView)findViewById(R.id.grid01);
grid.setAdapter(ba);
}
}
从上面可以看到,定义了字符串,数字,尺寸,数组资源xml文件,在代码中能很容易的使用到它们,在动态生成的R资源类里面可以很轻松的找到它们,系统运行效果如下