Andriod(3)——Understanding Android Resources

Now, we will follow that introduction with an in-depth look at Android SDK fundamentals and cover resources , content providers, and intents. These three concepts are fundamental to understanding Android programming and should place you on a solid foundation for the material in subsequent chapters.


1 Understanding Resources

You can change a resource without recompiling the application,  and a resource in Android is a file or a value that is bound to an executable application.

Every resource has a ID, that you can change the content of the ID without changing the source code.

1 String Resource

String resource definitions is in some XML files which reside in the subdirectory as follows:

res/values
When the file is created or updated, the Eclipse ADT plug-in will automatically create or update a Java class in your applications's root package called R.java with unique IDs for the two string resources specified.

R.java just like this:

The two static final ints defined with variable names hello and app_name are the resource IDs that represent the corresponding string resources. You could use these resource iDs anywhere in the source code through the following code structure:

R.string.hello

2 Layout Resources

In Android, the view of a screen is often loaded from an XML file as resource.These XML files are called layout resources. A layout resource is a key resource used in Android UI programming. 

setContentView(R.layout.main);

This line points out htat there is a static class called R.layout, and within that class, there is a constant called main(an integer), pointing to a View defined by an XML layout resource file.  This statement expects the programmer to create the file /res/layout/main.xml and place the necessary layout definition in that file.

A LinearLayout lays out its children vertically or horizontally.

You will need to define a separate layout file for each screen (or activity). More accurately, each layout needs a dedicated file.

The views defined in layout files are accessible in java code through their resource IDs generated in R.java.

TextView tv = (TextView)this.findViewById(R.id.text1); 
tv.setText("Try this text instead"); 

The constant R.id.text1 corresponds to the ID defined for the TextView. The id for the TextView in the layout file is as follows:

<TextView android:id="@+id/text1"
.. 
</TextView> 

The constant called text1 will be used to uniquely identify this view among other views hosted by that activity. The plus sign (+) in @+id/text1 means that the ID text1 will be created if it doesn't exist already.

3 Resource Reference Syntax

The syntax you use to allocate an id to a resource in the XML file is called resource-reference syntax.

The id attribute syntax in the previous example @+id/text1 has the following formal structure:

@[package:]type/name


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值