Android学习二:Resources

Resources在Android体系中非常重要,常用的Resources有strings,colors,bitmaps和layouts等,你可以修改这些文件的值而不用去重新编译程序。在android中有非常多的resource类型,在这里我们主要讨论学习常用的类型。

  • String Resources

     android的strings文件放在res/values目录下面,你可以定义一个或多个XML文件来存放strings值,文件名可以随意取,不过经常情况下你看到的文件名是

strings.xml。

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">hello</string>
<string name="app_name">hello appname</string>
</resources>

在程序中使用时:

getString(R.string.app_name);
getString(R.string.hello);

还可以在xml文件中这样用:android:text="@string/app_name"

  • Layout Resources

    在android中窗口的布局通常都是通过xml文件来完成的,文件一般放在res/layout目录下面。

public class HelloWorldActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView)this.findViewById(R.id.text1);
tv.setText("Try this text instead");
}
...
}

代码中红色部分说明设置此Activity的view的布局文件是layout下的main.xml文件。

比如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:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:id="@+id/b1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>

  • 引用Resources

Resource的引用语法结构是:@[package:]type/name。常用的

  • R.string
  • R.id
  • R.drawable
  • R.layout
  • R.attr
  • R.plural
  • R.array

在res目录下有一些重要的文件类型目录我们需要知道:

  • anim:  编译动画文件。
  • drawable:  Bitmaps.
  • layout: 定义UI和view。
  • values:Arrays,strings,colors,dimensions和styles。
  • xml: xml文件。
  • raw:Noncompiles raw files。
  • String Arrays的用法
<resources ....>
......Other resources
<string-array name="test_array">
<item>one</item>
<item>two</item>
<item>three</item>
</string-array>
......Other resources
</resources>

在代码中引用时:

//Get access to Resources object from an Activity
Resources res = your-activity.getResources();
String strings[] = res.getStringArray(R.array.test_array);

  • Plurals
<resources...>
<plurals name="eggs_in_a_nest_text">
<item quantity="one">There is 1 egg</item>
<item quantity="other">There are %d eggs</item>
</plurals>
</resources>
Resources res = your-activity.getResources();
String s1 = res.getQuantityString(R.plurals.eggs_in_a_nest_text, 0,0);
String s2 = res.getQuantityString(R.plurals.eggs_in_a_nest_text, 1,1);
String s3 = res.getQuantityString(R.plurals.eggs_in_a_nest_text, 2,2);
String s4 = res.getQuantityString(R.plurals.eggs_in_a_nest_text, 10,10);

 

  • Colors 资源

 

xml文件中定义colors:

<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>

在java代码中引用:

int mainBackGroundColor
= activity.getResources.getColor(R.color.main_back_ground_color);

还可以在view布局文件中使用:

<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/red"
android:text="Sample Text to Show Red Color"/>
  • Dimensions(尺寸) 资源
<resources>
<dimen name="mysize_in_pixels">1px</dimen>
<dimen name="mysize_in_dp">5dp</dimen>
<dimen name="medium_size">100sp</dimen>
</resources>
float dimen = activity.getResources().getDimension(R.dimen.mysize_in_pixels);
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/medium_size"/>
  • Image 资源
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Dial"
android:background="@drawable/sample_image"
/>
//Call getDrawable to get the image
BitmapDrawable d = activity.getResources().getDrawable(R.drawable.sample_image);
//You can use the drawable then to set the background
button.setBackgroundDrawable(d);
//or you can set the background directly from the Resource Id
button.setBackgroundResource(R.drawable.sample_image);
  • 读取XML文件

读取res/xml/test.xml

<rootelem1>
<subelem1>
Hello World from an xml sub element
</subelem1>
</rootelem1>
private String getEventsFromAnXMLFile(Activity activity)
throws XmlPullParserException, IOException
{
StringBuffer sb = new StringBuffer();
Resources res = activity.getResources();
XmlResourceParser xpp = res.getXml(R.xml.test);
xpp.next();
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
if(eventType == XmlPullParser.START_DOCUMENT)
{
sb.append("******Start document");
}
else if(eventType == XmlPullParser.START_TAG)
{
sb.append("\nStart tag "+xpp.getName());
}
else if(eventType == XmlPullParser.END_TAG)
{
sb.append("\nEnd tag "+xpp.getName());
}
else if(eventType == XmlPullParser.TEXT)
{
sb.append("\nText "+xpp.getText());
}
eventType = xpp.next();
}//eof-while
sb.append("\n******End document");
return sb.toString();
}//eof-function
  • Raw Resources

读取res/raw/test.txt文件

String getStringFromRawFile(Activity activity)
throws IOException
{
Resources r = activity.getResources();
InputStream is = r.openRawResource(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();
}
  • Assets

Assets目录与res目录同级,放入assets目录下的文件不会被编译无法通过R.java访问,需要通过AssetManager对象来访问。

//Note: Exceptions are not shown in the code
String getStringFromAssetFile(Activity activity)
{AssetManager am = activity.getAssets();
InputStream is = am.open("test.txt");
String s = convertStreamToString(is);
is.close();
return s;
}

好了,我们重新看下Resources的文件结构:

/res/values/strings.xml
/colors.xml
/dimens.xml
/attrs.xml
/styles.xml
/drawable/*.png
/*.jpg
/*.gif
/*.9.png
/anim/*.xml
/layout/*.xml
/raw/*.*
/xml/*.xml
/assets/*.*/*.*

posted @ 2015-07-27 08:25  古韵古风  阅读(328)  评论(0编辑  收藏  举报