Chrisの梳羽之礁

A look of quick intelligence and soft refinement
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Android资源的别名

Posted on 2012-06-27 11:17  Chrisfang6  阅读(1370)  评论(0编辑  收藏  举报

 

很多资源,比如图片,在不同的资源文件夹下可能是重复的。可以使用别名使其只有一份源文件。

Not all resources offer a mechanism by which you can create an alias to another resource. In particular, animation, menu, raw, and other unspecified resources in the xml/ directory do not offer this feature.

 

 1. drawable: 属性元素关键字 bitmap

1 <?xml version="1.0" encoding="utf-8"?>   
2 <bitmap xmlns:android="http://schemas.android.com/apk/res/android"  
3     android:src="@drawable/icon_alias" />  

 

 这样就可以通过R.drawable.icon来引用这个别名图标,实际上是指向drawable/icon_alias.jpg.

 

2. layout:

 

1 <?xml version="1.0" encoding="utf-8"?>
2 <merge>
3     <include layout="@layout/main_ltr"/>
4 </merge>

 

 

http://developer.android.com/training/multiscreen/screensizes.html#TaskUseAliasFilters

  • res/layout/main.xml, single-pane layout
  • res/layout/main_twopanes.xml, two-pane layout

And add these two files:

  • res/values-large/layout.xml:
    1 <resources>
    2     <itemname="main"type="layout">@layout/main_twopanes</item>
    3 </resources>

     

  • res/values-sw600dp/layout.xml:
    1 <resources>
    2     <itemname="main"type="layout">@layout/main_twopanes</item>
    3 </resources>

     

These latter two files have identical content, but they don’t actually define the layout. They merely set up main to be an alias to main_twopanes. Since these files have large and sw600dp selectors, they are applied to tablets and TVs regardless of Android version (pre-3.2 tablets and TVs match large, and post-3.2 will match sw600dp).