package com.test.valuesresourcedemo;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;
/**
* 使用字符串,颜色,尺寸 资源文件
*/
public class ValuesResourceActivity extends AppCompatActivity {
//使用字符串资源
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
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_values_resource);
BaseAdapter adapter = new BaseAdapter() {
@Override
public int getCount() {
return textIds.length;
}
@Override
public Object getItem(int position) {
return getResources().getText(textIds[position]);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView text = new TextView(ValuesResourceActivity.this);
Resources res = ValuesResourceActivity.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.setGravity(Gravity.CENTER);
text.setTextSize(getResources().getInteger(R.integer.font_size));
return text;
}
};
GridView gridView = (GridView) findViewById(R.id.grid001);
gridView.setAdapter(adapter);
}
}
资源文件
颜色资源文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<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>
字符串文件资源
<resources>
<string name="app_name">字符串,数字,尺寸资源文件</string>
<string name="c1">人</string>
<string name="c2">之</string>
<string name="c3">初</string>
<string name="c4">性</string>
<string name="c5">本</string>
<string name="c6">善</string>
<string name="c7">性</string>
<string name="c8">向</string>
<string name="c9">远</string>
</resources>
字体资源文件
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<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>
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.test.valuesresourcedemo.ValuesResourceActivity">
<!--使用字符串,尺寸资源-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/app_name"
android:textSize="@dimen/title_font_size"/>
<!--定义一个 GridView组件,
使用尺寸资源中定义的长度来指定水平间距,垂直间距-->
<GridView
android:id="@+id/grid001"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:horizontalSpacing="@dimen/spacing"
android:numColumns="3"
android:verticalSpacing="@dimen/spacing"></GridView>
</LinearLayout>