下面是使用字符串,數(shù)字,尺寸,數(shù)組資源的例子,挺好,一個(gè)例子已經(jīng)囊括了不少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">字符串,數(shù)字,尺寸,數(shù)組資源</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://schemas.android.com/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);
//創(chuàng)建一個(gè)BaseAdapter對(duì)象
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();
//使用尺寸資源來設(shè)置文本框的高度,寬度
text.setWidth((int)res.getDimension(R.dimen.cell_width));
text.setHeight((int)res.getDimension(R.dimen.cell_height));
//使用字符串資源設(shè)置文本框的內(nèi)容
text.setText(texts[position]);
TypedArray icons = res.obtainTypedArray(R.array.plain_arr);
//使用顏色資源來設(shè)置文本框的背景色
text.setBackgroundDrawable(icons.getDrawable(position));
text.setTextSize(20);
return text;
}
};
GridView grid = (GridView)findViewById(R.id.grid01);
grid.setAdapter(ba);
}
}
從上面可以看到,定義了字符串,數(shù)字,尺寸,數(shù)組資源xml文件,在代碼中能很容易的使用到它們,在動(dòng)態(tài)生成的R資源類里面可以很輕松的找到它們,系統(tǒng)運(yùn)行效果如下
初學(xué)Android,字符串,數(shù)字,尺寸,數(shù)組資源(十二)