package com.test.arrayresourceactivity;
import android.content.res.Resources;
import android.content.res.TypedArray;
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;
/**
* 数组(Array) 资源
*/
public class ArrayResourceActivity extends AppCompatActivity {
//获取系统定义的数组资源
String[] texts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_array_resource);
texts = getResources().getStringArray(R.array.string_arr);
//创建一个 BaseAdapter对象
BaseAdapter adapter = new BaseAdapter() {
@Override
public int getCount() {
return texts.length;
}
@Override
public Object getItem(int position) {
return texts[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = new TextView(ArrayResourceActivity.this);
Resources res = ArrayResourceActivity.this.getResources();
//使用尺寸资源设置文本框的高度和 宽度
textView.setWidth((int) res.getDimension(R.dimen.cell_width));
textView.setHeight((int) res.getDimension(R.dimen.cell_height));
//设置文本框内容
textView.setText(texts[position]);
TypedArray icons = res.obtainTypedArray(R.array.palin_arr);
//使用颜色资源来设置文本框背景色
textView.setBackgroundDrawable(icons.getDrawable(position));
textView.setTextSize(20);
textView.setGravity(Gravity.CENTER);
return textView;
}
};
GridView gridView = (GridView) findViewById(R.id.grid01);
gridView.setAdapter(adapter);
}
}
布局文件
<?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:gravity="center_horizontal"
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.arrayresourceactivity.ArrayResourceActivity">
<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/grid01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:horizontalSpacing="@dimen/spacing"
android:numColumns="3"
android:verticalSpacing="@dimen/spacing"
></GridView>
<!--定义ListView组件 使用数组资源-->
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/books"></ListView>
</LinearLayout>
字符串资源
<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="title_font_size">20sp</dimen>
<dimen name="cell_width">60dp</dimen>
<dimen name="cell_height">66dp</dimen>
</resources>
颜色资源
<?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>
数组资源
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--定义一个Drawable数组-->
<array name="palin_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>Android 讲义</item>
<item>Android Studio 讲义</item>
</string-array>
</resources>