[Android]偽名機能を使用して、同じリソースの重複を防止します。

6521 ワード

アプリケーションのために異なるリソースファイルと整合する場合、異なる適応型のリソースパスで同じリソースファイルを使用する必要がある場合があります。この場合、alias方法を使用すると、同じリソースファイルの重複を防止し、効率を向上させることができます。以下はAndroid開発ドキュメントhttp://developer.android.com/guide/topics/resources/providing-resources.html#AliasResources
Creating alias resoures
When you have a resource that you'd like to use for more than one device configration(but dot not want to provide as a default reource)、you dot need to put the same reource in alternative resource.castory。create an alternative resource that acts as alias for a resource saved in your default reource directory.
Note:Not all resource offer a mechaism by which you can create an alias to an other resource.In particular,animation,menu,raw,and other unspecifed reource in thexml/directory not feath.
For example、imgine you have aapaplication、icon.png、and need unique version of it for differentlocaes.However、twolocars、English-nadian and Freench-nadian、need to use the same the same sion.Youmimimithattttttttttttttttttttttttttttttttttttnerererererererererererererererererererererererererererererererererererererererererererererererererererererererererean,but it's not true.Instead,you can save the mage that's used for both asicon_ca.png(any name other thanicon.png)and put it in the defaultres/drawable/directory.The n create anicon.xmlfile inres/drawable-en-rCA/andres/drawable-fr-rCA/that refers to theicon_ca.pngreroue usinthe<bitmap>elemento.Thislolowlowlowlowwlowslolololowststststininininininininininininatotototototototototototototototototototototorererererererererererererererererererereaaatototototototototototototototototomomomomomomomomomomomo..。
Drawable
To create an alias to an existing drawable、use the<bitmap>element.For example:
<?xml version="1.0" encoding="utf-8"?>
<bitmapxmlns:android="http://schemas.android.com/apk/res/android"
   
android:src="@drawable/icon_ca"/>
If you save this file asicon.xml(in alternative resource directory、such asres/drawable-en-rCA/)、it is copiled into a reource can reference asR.drawable.icon、but is actually an for the・45916。
Layout
To create an alias to an existing layout、use theR.drawable.icon_caelement、wrapped in ares/drawable/・For example:
<?xml version="1.0" encoding="utf-8"?>
<merge>
   
<includelayout="@layout/main_ltr"/>
</merge>
If you save this file as<include>、it is copiled into a reource you can reference as<merge>、but is actually an alias for themain.xmlrercure.
Strings and other simple values
To create an alias to an existing string、simply use the reource ID of the desired string as the value for the new string.For example:
<?xml version="1.0" encoding="utf-8"?>
<resources>
   
<stringname="hello">Hello</string>
   
<stringname="hi">@string/hello</string>
</resources>
TheR.layout.mainreource is now an alias for theR.layout.main_ltrOther simple valueswork the same way.For example、a:
<?xml version="1.0" encoding="utf-8"?>
<resources>
   
<colorname="yellow">#f00</color>
   
<colorname="highlight">@color/red</color>
</resources>

xml , 、 、 。