Android - 资源的定义和引用
来源:互联网 发布:淘宝网在哪里买基金 编辑:程序博客网 时间:2024/05/16 09:13
Android中的字符串、颜色值、数组等资源多是定义在xml文件中,ADT会为每个资源生成一个ID并存放在R.java中。代码中,我们就使用R.java的这些资源的ID。
定义字符串(res/values/strings.xml)
- <string name="hello">Hello</string>
在代码中取得其引用:
- getResources().getString(R.string.hello);
在xml文件中取得其引用:
- android:text="@string/hello"
定义颜色(res/values/color.xml):
- <resources>
- <color name="red">#f00</color>
- <color name="blue">#0000ff</color>
- <color name="green">#f0f0</color>
- <color name="main_back_ground_color">#ffffff00</color>
- </resources>
在代码中取得其引用:
- activity.getResources.getColor(R.color.red);
在xml文件中取得其引用:
- android:textColor="@color/red"
定义数组(res/values/color.xml):
- <array name="integer_array">
- <item>1</item>
- <item>2</item>
- <item>3</item>
- </array>
在代码中取得其引用:
- int[] arr = getResources().getIntArray(R.array.integer_array);
定义字符数组(res/values/array.xml):
- <string-array name="drawings">
- <item>随笔</item>
- <item>直线</item>
- <item>矩形</item>
- <item>椭圆</item>
- <item>圆形</item>
- <item>圆点</item>
- <item>文字</item>
- </string-array>
在代码中取得其引用:
- String[] arr = getResources().getStringArray(R.array.drawings);
在xml文件中取得其引用:
- android:entryValues="@array/drawings"
定义style(res/values/*.xml)
- <mce:style name="nomarl_text"><!--
- <item name="android:textSize">8sp</item>
- <item name="android:textColor">#111</item>
- --></mce:style><style name="nomarl_text" mce_bogus="1"> <item name="android:textSize">8sp</item>
- <item name="android:textColor">#111</item></style>
在xml文件中取得其引用:
- android:theme="@style/nomarl_text"
定义View(res/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"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- </LinearLayout>
在代码中取得其引用:
- setContentView(R.layout.main);
定义menu(res/menu/menu_main.xml)
- <?xml version="1.0" encoding="utf-8"?>
- <menu xmlns:android="http://schemas.android.com/apk/res/android">
- <item
- android:title="设置"
- android:id="@+id/setting_menu_item"
- android:icon="@android:drawable/ic_menu_preferences">
- </item>
- <item
- android:title="保存"
- android:id="@+id/save_menu_item"
- android:icon="@android:drawable/ic_menu_save">
- </item>
- <item
- android:id="@+id/what_to_draw"
- android:title="图形">
- </item>
- </menu>
在代码中取得其引用:
- MenuInflater inflater = getMenuInflater();
- inflater.inflate(R.menu.menu_main, menu);
定义preferences(res/xml/settings.xml)
- <?xml version="1.0" encoding="utf-8"?>
- <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
- <PreferenceCategory
- android:title="@string/pen_title"
- android:key="@string/pen_key"
- android:summary="@string/pen_summary">
- <Preference
- android:summary="@string/pen_width_summary"
- android:title="@string/pen_width_title"
- android:key="@string/pen_width_key">
- </Preference>
- <Preference
- android:summary="@string/pen_color_summary"
- android:title="@string/pen_color_title"
- android:key="@string/pen_color_key">
- </Preference>
- <CheckBoxPreference
- android:summaryOn="@string/pen_style_summary_on"
- android:summaryOff="@string/pen_style_summary_off"
- android:title="@string/pen_style_title"
- android:key="@string/pen_style_key"
- android:defaultValue="@string/checkbox_default_false">
- </CheckBoxPreference>
- <CheckBoxPreference
- android:summaryOn="@string/pen_antialias_summay_on"
- android:summaryOff="@string/pen_antialias_summay_off"
- android:title="@string/pen_antialias_title"
- android:key="@string/pen_antialias_key"
- android:defaultValue="@string/checkbox_default_true">
- </CheckBoxPreference>
- </PreferenceCategory>
- <PreferenceCategory
- android:title="@string/canvas_title"
- android:key="@string/canvas_key">
- <Preference
- android:key="@string/canvas_bgcolor_key"
- android:title="@string/canvas_bgcolor_title"
- android:summary="@string/canvas_bgcolor_summary">
- </Preference>
- </PreferenceCategory>
- </PreferenceScreen>
在代码中取得其引用:
- addPreferencesFromResource(R.xml.settings);
0 0
- Android - 资源的定义和引用
- Android - 资源的定义和引用
- Android - 资源的定义和引用
- Android资源的定义和引用
- Android - 资源的定义和引用
- Android 关键资源的定义和使用
- android 引用资源时,?android和@android的区别
- android工程引用另外一个工程的类和资源
- android Xml文件引用资源中@和?的区别
- android 资源引用的例子
- android 资源引用的例子
- Android引用资源的方法
- Android 学习笔记4:资源文件的定义和使用
- Android定义资源的规则
- Android资源引用中“?”和“@”区别
- java中几种引用类型的定义和区别。强引用,软引用,弱引用,虚引用
- android 各种xml资源的引用方式
- android引用arrays下的数组资源
- IOS网络篇:HTTP的通信过程
- leetcode|Valid Number(65)
- iOS 自定义navigationBar上得字体属性
- 软件测试_JMeter自动化测试工具2_Jmeter压力测试(简单的http请求)-实例
- UITextField修改placeHolder颜色
- Android - 资源的定义和引用
- 【杭电oj】1090 - A+B for Input-Output Practice (II)
- 面试题52:不能被继承的类
- 如何选择 compileSdkVersion, minSdkVersion 和 targetSdkVersion
- ostep02--The Abstraction:The Process
- c++ 对象的复制与赋值
- android 点击Edittext时hint值不自动隐藏问题
- Android退出所有Activity最优雅的方式
- GIT - 日常操作(1)