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-2012, 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);
}
}
layout/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>
values/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>
values/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>
values/strings.xml
values/integers.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="font_size">20</integer>
</resources>