Android ApiDemos樣本解析(48):Content->Resources->R

來源:互聯網
上載者:User

Android SDK對應沒個定義在res目錄下的資源都會定義一個維一的資源ID。在編譯時間會在gen目錄下產生 R.java , 資源ID的格式為 R.[type].[id] 類型為資源類型如anim, array, attr等

 

可以在代碼或是XML資源檔(如layout, style中)訪問這些資源。

Context類提供了訪問資源的方法,一是直接通過如getText, getString等來訪問資源,此外一般的方法是通過getResources()來取代Resources對象,再通過Resources對象來訪問資源。為Context的類繼承圖:

 

可以看到Application ,Service ,Activity 都是Context的子類,因此在這些類中都可以直接存取資源:

[java] 
// Using the getString() conevenience method,   
// retrieve a string   
// resource that hapepns to have style information.   
//Note the use of   
// CharSequence instead of String so we don't   
// lose the style info.   
cs = getText(R.string.styled_text);  
tv = (TextView)findViewById(R.id.styled_text);  
tv.setText(cs);  
   
// Use the same resource, but convert it to a   
//string, which causes it   
// to lose the style information.   
str = getString(R.string.styled_text);  
tv = (TextView)findViewById(R.id.plain_text);  
tv.setText(str);  
   
// You might need to do this if your code   
// is not in an activity.   
// For example View has a protected mContext   
//field you can use.   
// In this case it's just 'this' since   
//Activity is a context.   
Context context = this;  
   
// Get the Resources object from our context   
Resources res = context.getResources();  
   
// Get the string resource, like above.   
cs = res.getText(R.string.styled_text);  
tv = (TextView)findViewById(R.id.res1);  
tv.setText(cs); 
// Using the getString() conevenience method,
// retrieve a string
// resource that hapepns to have style information.
//Note the use of
// CharSequence instead of String so we don't
// lose the style info.
cs = getText(R.string.styled_text);
tv = (TextView)findViewById(R.id.styled_text);
tv.setText(cs);
 
// Use the same resource, but convert it to a
//string, which causes it
// to lose the style information.
str = getString(R.string.styled_text);
tv = (TextView)findViewById(R.id.plain_text);
tv.setText(str);
 
// You might need to do this if your code
// is not in an activity.
// For example View has a protected mContext
//field you can use.
// In this case it's just 'this' since
//Activity is a context.
Context context = this;
 
// Get the Resources object from our context
Resources res = context.getResources();
 
// Get the string resource, like above.
cs = res.getText(R.string.styled_text);
tv = (TextView)findViewById(R.id.res1);
tv.setText(cs);

 

 作者:mapdigit
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.