1. XML资源文件中可以定义普通字符串,引用字符串,HTML 字符串和可替换字符串。
定义字符串资源的XML语法
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="simple_string">simple string</string>
<string name="quoted_string">"quoted 'xyz' string</string>
<string name="double_quoted_string">\"double quoted\"</string>
<string name="java_format_string">hello %2$s Java format string. %1$s again</string>
<string name="tagged_string">Hello <b><i>Slanted Android</i></b>,you are bold</string>
</resources>
</pre><pre code_snippet_id="426151" snippet_file_name="blog_20140714_3_1883724" name="code" class="html">在java代码中使用字符串资源
<pre name="code" class="java">
String simpleString=activity.getString(R.string.simple_string);
String quotedString=activity.getString(R.string.quoted_string);
String doubleQuotedString=activity.getString(R.string.double_quoted_string);
String javaFormatString=activity.getString(R.string.java_format_string);
String substitutedString=String.format(javaFormatString, "Hello","Android");
String htmlTaggedString=activity.getString(R.string.tagged_string);
Spanned textSpan=android.text.Html.fromHtml(htmlTaggedString);
textView.setText(textSpan);
2.定义颜色资源的XML
示例:
<resources>
<color name="red">#foo</color>
<color name="blue">#ooooff</color>
<color name="green">#fofo</color>
<color name="main_back_ground_color">#ffffff00</color>
</resources>
int mainBackgroundColor=activity.getResources.getColor(R.color.main_back_ground_color);
</pre><pre code_snippet_id="426151" snippet_file_name="blog_20140714_8_8787972" name="code" class="html"><TextView android:id="@+id/txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textColor="@color/red"
/>
参阅了颜色的代码
统计下android:的类型,方便下次查阅
<TextView android:id="@+id/txt"
android:layout_width=""
android:layout_height=""
android:layout_weight=""
android:layout_gravity=""
android:layout_margin=""
android:layout_marginLeft=""
android:layout_marginTop=""
android:layout_marginRight=""
android:layout_marginBottom=""
android:layout_marginStart=""
android:layout_marginEnd=""
android:lineSpacingMultiplier=""
android:fontFamily=""
android:textColorLink=""
android:cursorVisible=""
android:maxLines=""
android:maxHeight=""
android:lines=""
android:height=""
android:minLines=""
android:minHeight=""
android:maxEms=""
android:maxWidth=""
android:ems=""
android:width=""
android:minEms=""
android:minWidth=""
android:gravity=""
android:scrollHorizontally=""
android:password=""
android:singleLine=""
android:enabled=""
android:selectAllOnFocus=""
android:includeFontPadding=""
android:maxLength=""
android:shadowColor=""
android:shadowDx=""
android:shadowDy=""
android:shadowRadius=""
android:autoLink=""
android:linksClickable=""
android:numeric=""
android:digits=""
android:phoneNumber=""
android:inputMethod=""
android:capitalize
android:autoText
android:editable
android:freezesText
android:ellipsize
android:drawableTop
android:drawableBottom
android:drawableRight
android:drawableStart
android:drawableEnd
android:drawablePadding
android:lineSpacingExtra
android:lineSpacingMultiplier
android:marqueeRepeatlimit
android:inputType
android:imeOptions
android:privateImeOptions
android:imeActionLabel
android:imeActionId
android:editorExtras
android:textSelectHandleLeft
android:textSelectHandleRight
android:textSelectHandle
android:textEditPasteWindowLayout
android:textEditNoPasteWindowLayout
android:textEditSuggestionItemLayout
android:textCursorDrawable
android:textIsSelectable
android:textAllCaps
android:id
android:tag
android:scrollX
android:scrollY
android:background
android:padding
android:paddingLeft
android:paddingTop
android:paddingRight
android:paddingBottom
android:paddingStart
android:paddingEnd
android:focusableInTouchMode
android:visibility
android:fitsSystemWindows
android:scrollbarStyle
android:isScrollContainer
android:fadeScrollbars
android:scrollbarFadeDuration
android:scrollbarDefaultDelayBeforeFade
android:scrollbarSize
android:scrollbarThumbHorizontal
android:scrollbarThumbVertical
android:scrollbarThumbVertical
android:scrollbarTrackHorizontal
android:scrollbarTrackVertical
android:scrollbarAlwaysDrawHorizontalTrack
android:scrollbarAlwaysDrawVerticalTrack
android:fadingEdge
android:requiresFadingEdge
android:fadingEdgeLength
android:nextFocusLeft
android:nextFocusRight
android:nextFocusUp
android:nextFocusDown
android:newxtFocusForward
android:clickable
android:longClickable
android:saveEnabled
android:filterTouchesWhenObscured
android:drawingCacheQuality
android:keepScreenOn
android:duplicateParentState
android:minHeight
android:minWidth
android:soundEffectsEnable
android:hapticFeedbackEnabled
android:contentDescription
android:onClick
android:overScrollMode
android:alpha
android:translationX
android:translationY
android:transformPivotX
android:transformPivotY
android:rotation
android:rotationX
android:rotationY
android:scaleX
android:scaleY
android:verticalScrollbarPosition
android:layerType
android:layoutDirection
android:textDirection
android:textAlignment
android:importantForAccessibility
android:accessibilityLiveRegion
android:lableFor
android:bufferType
android:text
android:hint
android:textColor
android:textColorHighlight
android:textColorHint
android:textAppearance
android:textSize
android:textScaleX
android:typeface<pre name="code" class="html">android:textStyle
</pre><pre code_snippet_id="426151" snippet_file_name="blog_20140714_8_7880589" name="code" class="html">
/>
3.定义尺寸XML
<resources>
<dimen name="mysize_in_pixels">1px</dimen>>
<dimen name="mysize_in_dp">5dp</dimen>
<dimen name="medium_size">100sp</dimen>
</resources>
px:像素
in:英寸
mm:毫米
pt:磅
dp:与密度无关的像素,基于160dpi(每英寸的像素数)屏幕(尺寸适应屏幕密度)
sp:与比例无关的像素(这种尺寸支持用户调整大小,适合在字体中使用)
float dimen=activity.getResources().getDimension(R.dimen.mysize_in_pixels);
<TextView android:textSize="@dimen/medium_size"
/>
4.图像资源
在xml中布局
<Button
android:background="@drable/sample_image"
/>
或者在java中获得
BitmapDrawable d=activity.getResources().getDrawable(R.drawable.sample_image);
button.setBackgroundDrawable(d);
button.setBackgroundResource(R.drawable.sample_image);
注:.png图片可拉伸
5.获取XML
Resources res=activity.getResources();
XmlResourceParser xpp=res.getXml(R.xml.test);
6.使用原始资源
String getStringFromRawFile(Activity activity)throws IOException
{
Resources r=activity.getResources();
InputStream is=r.openRawResourceFd(R.raw.test);
String myText=convertStreamToString(is);
is.close();
return myText;
}
String convertStreamToString(InputStream is)throws IOException
{
ByteArrayOutputStream baos=new ByteArrayOutputStream();
int i=is.read();
while(i!=-1){
baos.write(i);
i=is.read();
}
return baos.toString();
}
7.使用assets
assets和/res同样级别,未包含在/res子目录中。/assets中的文件不会在R.java中生成ID,必须指定以/assets开头的文件路径才能读取,使用AssetManager来访问
String getStringFromAssetFile(Activity activity)
{
AssetManager am=activity.getAssets();
InputStream is=am.open("test.txt");
String s=convertStreamToString(is);
is.close();
return s;
}