Android資源應用技巧剖析

來源:互聯網
上載者:User

在上一篇文章中,我們為大家詳細介紹了有關Android Menu編程方式解析的內容,來協助大家理解Android這一作業系統在介面處理上的相關操作。那麼在這篇文章中我們將會針對Android資源的相關概念為大家詳細講解有關介面布局的一些應用,加深大家對介面處理的理解。

1.添加菜單menu.add(0, Menu.FIRST+1, 1, R.string.menu_open);

menu.add(0, Menu.FIRST+2, 2, R.string.menu_edit);代碼中的 R.string.menu_open/menu_edit

這些其實是指Android資源檔中的ID,映射到具體的資源,這裡是映射到字串資源menu_open, menu_edit,其具體的值可以看res/values/string.xml在這裡定義了字串的值:

 
  1. < ?xml version="1.0" encoding="utf-8"?> 
  2. < resources> 
  3. < string name="hello">Hello World, HelloActivity!< /string> 
  4. < string name="app_name">HelloWorld< /string> 
  5. < string name="menu_open">Open< /string> 
  6. < string name="menu_edit">Edit< /string> 
  7. < string name="menu_update">Update< /string> 
  8. < string name="menu_close">Close< /string> 
  9. < /resources> 

在Android中,Activity顯示的布局也可在Android資源中定義,並且以可視化的方式來操作布局對應的XML檔案。可以看res/layout/main.xml這就是一個布局檔案,這裡指定了這個布局裡有哪些介面元素以及如何組織,相對位置,絕對位置等資訊。來看看其中內容:

 
  1. < ?xml version="1.0" encoding="utf-8"?> 
  2. < LinearLayout xmlns:android="http://
    schemas.android.com/apk/res/android" 
  3. android:orientation="vertical" 
  4. android:layout_width="fill_parent" 
  5. android:layout_height="fill_parent" 
  6. < TextView android:id="@+id/TextView01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="MyTest OK yest!">< /TextView> 
  7. < /LinearLayout> 

這裡就描述了布局為LinearLayout,包含了一個TextView,TextView的值為 MyTest.這個XML檔案被編譯後,可以使用R.layout.main的ID來從資源中取得。

於是Activity可以用setContentView(R.layout.main)來直接從Android資源取得布局,來繪製介面元素。

另一類常用的Android資源就是圖片在res/drawable/下面有一些圖片,你也可以新加一些圖片到這裡。然後就可以通過.R.drawable.xxx 的ID來從資源中取得對應的圖片。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.