ValuesResTest--使用字符串、字体大小、颜色资源
2012-05-24 10:20
387 查看
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>
相关文章推荐
- iOS使用NSMutableAttributedString改变字符串中部分文字的字体颜色或大小
- 如何修改Recovery的字符串资源(修改显示字体颜色和大小)
- 我给女朋友讲编程CSS系列(3) CSS如何设置字体的类型、大小、颜色,如何使用火狐浏览器的Firebug插件查看网页的字体
- iOS开发—使用TYAttributedLabel实现字体大小颜色各异并且带链接的文本
- ios使用 KVC 修改textField的placeholder的字体颜色和大小
- 实验4 颜色、字符串资源的使用 实验报告
- 实验4 颜色、字符串资源的使用 实验报告
- TTTAttributedLabel第三方使用(一段连续的字符串不同范围用不同字体和颜色)
- UIAlertController的使用以及对字体颜色大小的修改
- 使用字符串、颜色、尺寸资源
- UI基础_UIAlertController/UIAlertView使用和字体大小颜色
- Android资源文件 - 使用资源存储字符串 颜色 尺寸 整型 布尔值 数组
- Android资源文件 - 使用资源存储字符串 颜色 尺寸 整型 布尔值 数组
- Android资源之String (三): html标签,语法(原生支持) 设置字体大小/颜色/下划线等
- iOS 界面上绘制不同字体 颜色 大小的字符串
- 实验4颜色、字符串资源的使用
- UIAlertViewController 基本使用+自定义(字体颜色,大小,对齐方式)
- iOS设置导航与其标题的颜色及字体大小和系统默认TabBar的相关设置与使用方法
- iOS-修改字符串特定位子 字 体 颜色 字体大小