Home | History | Annotate | Download | only in resources
      1 page.title=Truy cp Ti nguyn
      2 parent.title=Ti nguyn ng dng
      3 parent.link=index.html
      4 @jd:body
      5 
      6 <div id="qv-wrapper">
      7 <div id="qv">
      8   <h2>Xem nhanh</h2>
      9   <ul>
     10     <li>Ti nguyn c th c tham chiu t m bng cc s nguyn t {@code R.java}, chng hn nh
     11 {@code R.drawable.myimage}</li>
     12     <li>Ti nguyn c th c tham chiu t cc ti nguyn bng cch s dng mt c php XML c bit, v d nh {@code
     13 &#64;drawable/myimage}</li>
     14     <li>Bn cng c th truy cp ti nguyn ng dng ca mnh bng cc phng php trong
     15 {@link android.content.res.Resources}</li>
     16   </ul>
     17 
     18   <h2>Lp kha</h2>
     19   <ol>
     20     <li>{@link android.content.res.Resources}</li>
     21   </ol>
     22 
     23   <h2>Trong ti liu ny</h2>
     24   <ol>
     25     <li><a href="#ResourcesFromCode">Truy cp Ti nguyn t M</a></li>
     26     <li><a href="#ResourcesFromXml">Truy cp Ti nguyn t XML</a>
     27       <ol>
     28         <li><a href="#ReferencesToThemeAttributes">Tham chiu cc thuc tnh kiu</a></li>
     29       </ol>
     30     </li>
     31     <li><a href="#PlatformResources">Truy cp Ti nguyn Nn tng</a></li>
     32   </ol>
     33 
     34   <h2>Xem thm</h2>
     35   <ol>
     36     <li><a href="providing-resources.html">Cung cp Ti nguyn</a></li>
     37     <li><a href="available-resources.html">Loi Ti nguyn</a></li>
     38   </ol>
     39 </div>
     40 </div>
     41 
     42 
     43 
     44 
     45 <p>Sau khi cung cp mt ti nguyn trong ng dng ca mnh ( cp trong <a href="providing-resources.html">Cung cp Ti nguyn</a>), bn c th p dng n bng cch
     46 tham chiu ID ti nguyn . Tt c ID ti nguyn c nh ngha trong lp {@code R} d n ca bn, do
     47 cng c {@code aapt} t ng khi to.</p>
     48 
     49 <p>Khi ng dng ca bn c bin dch, {@code aapt} khi to lp {@code R}, trong  cha
     50 ID ti nguyn cho tt c ti nguyn trong th mc {@code
     51 res/} ca bn. Vi mi loi ti nguyn, c mt lp con {@code R} (v d,
     52 {@code R.drawable} cho tt c ti nguyn c th v), v vi mi ti nguyn loi , c mt s nguyn
     53 tnh (v d, {@code R.drawable.icon}). S nguyn ny l ID ti nguyn m bn c th s dng
     54  truy xut ti nguyn ca mnh.</p>
     55 
     56 <p>Mc d lp {@code R} l ni cc ID ti nguyn c quy nh, bn s khng cn
     57 tm    khm ph mt ID ti nguyn. Mt ID ti nguyn lun bao gm:</p>
     58 <ul>
     59   <li><em>Loi ti nguyn</em>: Mi ti nguyn c nhm vo mt "loi," chng hn nh {@code
     60 string}, {@code drawable}, v {@code layout}.  bit thm v cc loi khc nhau, hy xem phn <a href="available-resources.html">Loi Ti nguyn</a>.
     61   </li>
     62   <li><em>Tn ti nguyn</em>, l, hoc: tn tp,
     63 khng bao gm phn m rng; hoc gi tr trong thuc tnh XML {@code android:name}, nu ti nguyn
     64  l mt gi tr n gin (chng hn nh mt xu).</li>
     65 </ul>
     66 
     67 <p>C hai cch  bn c th truy cp mt ti nguyn:</p>
     68 <ul>
     69   <li><strong>Trong m:</strong> S dng mt s nguyn tnh t mt lp con ca lp {@code R}
     70 ca bn, chng hn nh:
     71     <pre class="classic no-pretty-print">R.string.hello</pre>
     72     <p>{@code string} l loi ti nguyn v {@code hello} l tn ti nguyn. C nhiu
     73 API Android m c th truy cp cc ti nguyn ca bn khi bn cung cp mt ID ti nguyn theo nh dng ny. Xem
     74 <a href="#ResourcesFromCode">Truy cp Ti nguyn trong M</a>.</p>
     75   </li>
     76   <li><strong>Trong XML:</strong> S dng mt c php XML c bit m cng tng ng vi
     77 ID ti nguyn c nh ngha trong lp {@code R} ca bn, chng hn nh:
     78     <pre class="classic no-pretty-print">&#64;string/hello</pre>
     79     <p>{@code string} l loi ti nguyn v {@code hello} l tn ti nguyn. Bn c th s dng c php
     80 ny trong mt ti nguyn XML  bt k ni no c k vng mt gi tr m bn cung cp trong mt ti nguyn. Xem phn <a href="#ResourcesFromXml">Truy cp Ti nguyn t XML</a>.</p>
     81   </li>
     82 </ul>
     83 
     84 
     85 
     86 <h2 id="ResourcesFromCode">Truy cp Ti nguyn trong M </h2>
     87 
     88 <p>Bn c th s dng mt ti nguyn trong m bng cch chuyn ID ti nguyn nh mt tham s phng php. V
     89 d, bn c th t mt {@link android.widget.ImageView}  s dng ti nguyn {@code res/drawable/myimage.png}
     90 bng cch s dng {@link android.widget.ImageView#setImageResource(int) setImageResource()}:</p>
     91 <pre>
     92 ImageView imageView = (ImageView) findViewById(R.id.myimageview);
     93 imageView.setImageResource(<strong>R.drawable.myimage</strong>);
     94 </pre>
     95 
     96 <p>Bn cng c th truy xut cc ti nguyn ring l bng cc phng php trong {@link
     97 android.content.res.Resources}, theo  bn c th nhn c mt thc th
     98 bng {@link android.content.Context#getResources()}.</p>
     99 
    100 <div class="sidebox-wrapper">
    101 <div class="sidebox">
    102 <h2>Truy cp cc Tp Gc</h2>
    103 
    104 <p>Tuy khng thng gp, song bn c th cn truy cp cc tp v th mc gc ca mnh. Nu bn lm vy th vic
    105 lu cc tp ca bn trong {@code res/} s khng c tc dng vi bn, v cch duy nht  c mt ti nguyn t
    106 {@code res/}  l bng ID ti nguyn. Thay vo , bn c th lu cc ti nguyn ca mnh trong th mc
    107 {@code assets/}.</p>
    108 <p>Cc tp lu trong th mc {@code assets/} <em>khng</em> c cp ID ti nguyn
    109 , v th bn khng th tham chiu chng thng qua lp {@code R} hoc t cc ti nguyn XML. Thay vo , bn c th
    110 truy vn cc tp trong th mc {@code assets/} nh mt h thng tp bnh thng v c d liu th bng cch s dng
    111 {@link android.content.res.AssetManager}.</p>
    112 <p>Tuy nhin, nu tt c nhng g bn yu cu l kh nng c d liu th (chng hn nh mt tp video hoc m thanh),
    113 vy bn hy lu tp trong th mc {@code res/raw/} v c mt lung byte bng cch s dng {@link
    114 android.content.res.Resources#openRawResource(int) openRawResource()}.</p>
    115 
    116 </div>
    117 </div>
    118 
    119 
    120 <h3>C php</h3>
    121 
    122 <p>Sau y l c php  tham chiu mt ti nguyn trong m:</p>
    123 
    124 <pre class="classic no-pretty-print">
    125 [<em>&lt;package_name&gt;</em>.]R.<em>&lt;resource_type&gt;</em>.<em>&lt;resource_name&gt;</em>
    126 </pre>
    127 
    128 <ul>
    129   <li><em>{@code &lt;package_name&gt;}</em> l tn ca gi m ti nguyn nm trong  (khng
    130 bt buc khi tham chiu cc ti nguyn t gi ca chnh bn).</li>
    131   <li><em>{@code &lt;resource_type&gt;}</em> l lp con {@code R} cho loi ti nguyn.</li>
    132   <li><em>{@code &lt;resource_name&gt;}</em> hoc l tn tp ti nguyn
    133 khng c phn m rng hoc l gi tr thuc tnh {@code android:name} trong phn t XML (i vi cc gi tr
    134 n gin).</li>
    135 </ul>
    136 <p>Xem phn <a href="available-resources.html">Loi Ti nguyn</a> 
    137 bit thm thng tin v mi loi ti nguyn v cch tham chiu chng.</p>
    138 
    139 
    140 <h3>Trng hp s dng</h3>
    141 
    142 <p>C nhiu phng php chp nhn mt tham s ID ti nguyn v bn c th truy xut ti nguyn bng cch s dng
    143 cc phng php trong {@link android.content.res.Resources}. Bn c th ly mt thc th {@link
    144 android.content.res.Resources} bng {@link android.content.Context#getResources
    145 Context.getResources()}.</p>
    146 
    147 
    148 <p>Sau y l mt s v d v truy cp ti nguyn trong m:</p>
    149 
    150 <pre>
    151 // Load a background for the current screen from a drawable resource
    152 {@link android.app.Activity#getWindow()}.{@link
    153 android.view.Window#setBackgroundDrawableResource(int)
    154 setBackgroundDrawableResource}(<strong>R.drawable.my_background_image</strong>) ;
    155 
    156 // Set the Activity title by getting a string from the Resources object, because
    157 //  this method requires a CharSequence rather than a resource ID
    158 {@link android.app.Activity#getWindow()}.{@link android.view.Window#setTitle(CharSequence)
    159 setTitle}(getResources().{@link android.content.res.Resources#getText(int)
    160 getText}(<strong>R.string.main_title</strong>));
    161 
    162 // Load a custom layout for the current screen
    163 {@link android.app.Activity#setContentView(int)
    164 setContentView}(<strong>R.layout.main_screen</strong>);
    165 
    166 // Set a slide in animation by getting an Animation from the Resources object
    167 mFlipper.{@link android.widget.ViewAnimator#setInAnimation(Animation)
    168 setInAnimation}(AnimationUtils.loadAnimation(this,
    169         <strong>R.anim.hyperspace_in</strong>));
    170 
    171 // Set the text on a TextView object using a resource ID
    172 TextView msgTextView = (TextView) findViewById(<strong>R.id.msg</strong>);
    173 msgTextView.{@link android.widget.TextView#setText(int)
    174 setText}(<strong>R.string.hello_message</strong>);
    175 </pre>
    176 
    177 
    178 <p class="caution"><strong>Ch :</strong> Bn khng nn sa i tp {@code
    179 R.java} bng cch th cng&mdash;n c khi to bi cng c {@code aapt} khi d n ca bn c
    180 bin dch. Mi thay i u b ghi  vo ln bin dch ti ca bn.</p>
    181 
    182 
    183 
    184 <h2 id="ResourcesFromXml">Truy cp Ti nguyn t XML</h2>
    185 
    186 <p>Bn c th nh ngha cc gi tr cho mt s thuc tnh v phn t XML bng cch s dng mt
    187 tham chiu ti mt ti nguyn hin c. Bn s thng lm iu ny khi to cc tp b tr, 
    188 cung cp cc xu v hnh nh cho widget ca mnh.</p>
    189 
    190 <p>V d, nu thm mt {@link android.widget.Button} vo b tr ca mnh, bn nn s dng
    191 mt <a href="string-resource.html">ti nguyn xu</a> cho vn bn nt:</p>
    192 
    193 <pre>
    194 &lt;Button
    195     android:layout_width="fill_parent"
    196     android:layout_height="wrap_content"
    197     android:text="<strong>@string/submit</strong>" /&gt;
    198 </pre>
    199 
    200 
    201 <h3>C php</h3>
    202 
    203 <p>Sau y l c php  tham chiu mt ti nguyn trong mt ti nguyn XML:</p>
    204 
    205 <pre class="classic no-pretty-print">
    206 &#64;[<em>&lt;package_name&gt;</em>:]<em>&lt;resource_type&gt;</em>/<em>&lt;resource_name&gt;</em>
    207 </pre>
    208 
    209 <ul>
    210   <li>{@code &lt;package_name&gt;} l tn ca gi m ti nguyn nm trong  (khng
    211 bt buc khi tham chiu cc ti nguyn t cng gi )</li>
    212   <li>{@code &lt;resource_type&gt;} l lp con
    213 {@code R} cho loi ti nguyn.</li>
    214   <li>{@code &lt;resource_name&gt;} hoc l tn tp ti nguyn
    215 khng c phn m rng hoc l gi tr thuc tnh {@code android:name} trong phn t XML (i vi cc gi tr
    216 n gin).</li>
    217 </ul>
    218 
    219 <p>Xem phn <a href="available-resources.html">Loi Ti nguyn</a> 
    220 bit thm thng tin v mi loi ti nguyn v cch tham chiu chng.</p>
    221 
    222 
    223 <h3>Trng hp s dng</h3>
    224 
    225 <p>Trong mt s trng hp bn phi s dng mt ti nguyn cho mt gi tr trong XML (v d,  p dng mt hnh nh c th v
    226 cho mt widget), nhng bn cng c th s dng mt ti nguyn trong XML  bt k ni no chp nhn mt gi tr n gin. V
    227 d, nu bn c tp ti nguyn sau bao gm mt <a href="more-resources.html#Color">ti nguyn mu</a> v mt <a href="string-resource.html">ti nguyn xu</a>:</p>
    228 
    229 <pre>
    230 &lt;?xml version="1.0" encoding="utf-8"?>
    231 &lt;resources>
    232    &lt;color name="opaque_red">#f00&lt;/color>
    233    &lt;string name="hello">Hello!&lt;/string>
    234 &lt;/resources>
    235 </pre>
    236 
    237 <p>Bn c th s dng nhng ti nguyn ny trong tp b tr sau  t mu vn bn v
    238 xu vn bn:</p>
    239 
    240 <pre>
    241 &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
    242 &lt;EditText xmlns:android=&quot;http://schemas.android.com/apk/res/android";
    243     android:layout_width=&quot;fill_parent&quot;
    244     android:layout_height=&quot;fill_parent&quot;
    245     android:textColor=&quot;<strong>&#64;color/opaque_red</strong>&quot;
    246     android:text=&quot;<strong>&#64;string/hello</strong>&quot; /&gt;
    247 </pre>
    248 
    249 <p>Trong trng hp ny, bn khng cn quy nh tn gi trong tham chiu ti nguyn  v ti nguyn
    250 xut pht t gi ca chnh bn. 
    251 tham chiu mt ti nguyn h thng, bn s cn a vo tn gi. V d:</p>
    252 
    253 <pre>
    254 &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
    255 &lt;EditText xmlns:android=&quot;http://schemas.android.com/apk/res/android";
    256     android:layout_width=&quot;fill_parent&quot;
    257     android:layout_height=&quot;fill_parent&quot;
    258     android:textColor=&quot;<strong>&#64;android:color/secondary_text_dark</strong>&quot;
    259     android:text=&quot;&#64;string/hello&quot; /&gt;
    260 </pre>
    261 
    262 <p class="note"><strong>Lu :</strong> Bn nn s dng cc ti nguyn xu 
    263 vo mi lc,  ng dng ca bn c th c bn a ha cho cc ngn ng khc. 
    264  bit thng tin v vic to cc ti nguyn
    265 thay th (chng hn nh xu c bn a ha), hy xem phn <a href="providing-resources.html#AlternativeResources">Cung cp Ti nguyn
    266 Thay th</a>.  c hng dn y  v vic bn a ha ng dng ca bn cho cc ngn ng khc,
    267 hy xem phn <a href="localization.html">Bn a ha</a>.</p>
    268 
    269 <p>Bn thm ch c th s dng ti nguyn trong XML  to cc b danh. V d, bn c th to mt ti nguyn c th v
    270 l mt b danh cho mt ti nguyn c th v khc:</p>
    271 
    272 <pre>
    273 &lt;?xml version="1.0" encoding="utf-8"?>
    274 &lt;bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    275     android:src="@drawable/other_drawable" />
    276 </pre>
    277 
    278 <p>Nghe c v tha, nhng c th rt hu ch khi s dng ti nguyn thay th. c thm v
    279 <a href="providing-resources.html#AliasResources">To ti nguyn b danh</a>.</p>
    280 
    281 
    282 
    283 <h3 id="ReferencesToThemeAttributes">Tham chiu cc thuc tnh kiu</h3>
    284 
    285 <p>Mt ti nguyn thuc tnh kiu s cho php bn tham chiu gi tr
    286 ca mt thuc tnh trong ch  ang p dng. Tham chiu mt thuc tnh kiu s cho php bn
    287 ty chnh din mo ca cc phn t UI bng cch to kiu cho chng  ph hp vi cc bin i tiu chun c cung cp bi
    288 ch  hin ti, thay v cung cp mt gi tr c m ha c nh. Tham chiu mt thuc tnh kiu
    289 v c bn m ni, l "s dng kiu c nh ngha bi thuc tnh ny, trong ch  hin ti."</p>
    290 
    291 <p> tham chiu mt thuc tnh kiu, c php tn gn nh tng t vi nh dng ti nguyn thng
    292 , nhng thay v biu tng @ ({@code @}), hy s dng mt du hi ({@code ?}), v
    293 phn loi ti nguyn l ty chn. V d:</p>
    294 
    295 <pre class="classic">
    296 ?[<em>&lt;package_name&gt;</em>:][<em>&lt;resource_type&gt;</em>/]<em>&lt;resource_name&gt;</em>
    297 </pre>
    298 
    299 <p>V d, sau y l cch bn c th tham chiu mt thuc tnh  t mu vn bn cho ph hp vi mu vn bn
    300 "chnh" ca ch  h thng:</p>
    301 
    302 <pre>
    303 &lt;EditText id=&quot;text&quot;
    304     android:layout_width=&quot;fill_parent&quot;
    305     android:layout_height=&quot;wrap_content&quot;
    306     android:textColor=&quot;<strong>?android:textColorSecondary</strong>&quot;
    307     android:text=&quot;&#64;string/hello_world&quot; /&gt;
    308 </pre>
    309 
    310 <p> y, thuc tnh {@code android:textColor} quy nh tn ca mt thuc tnh kiu
    311 trong ch  hin ti. Hin nay, Android s dng gi tr c p dng cho thuc tnh kiu {@code android:textColorSecondary}
    312 lm gi tr cho {@code android:textColor} trong widget ny. V cng c ti nguyn
    313 h thng bit rng mt ti nguyn thuc tnh s c yu cu trong ng cnh ny,
    314 bn khng cn nu r loi (m s l
    315 <code>?android:attr/textColorSecondary</code>)&mdash;bn c th khng nu loi {@code attr}.</p>
    316 
    317 
    318 
    319 
    320 <h2 id="PlatformResources">Truy cp Ti nguyn Nn tng</h2>
    321 
    322 <p>Android bao gm nhiu ti nguyn tiu chun, chng hn nh kiu, ch  v b tr. 
    323 truy cp cc ti nguyn ny, hy xc nh tham chiu ti nguyn ca bn bng tn gi
    324 <code>android</code>. V d, Android cung cp mt ti nguyn b tr bn c th s dng cho
    325 cc mc danh sch trong mt {@link android.widget.ListAdapter}:</p>
    326 
    327 <pre>
    328 {@link android.app.ListActivity#setListAdapter(ListAdapter)
    329 setListAdapter}(new {@link
    330 android.widget.ArrayAdapter}&lt;String&gt;(this, <strong>android.R.layout.simple_list_item_1</strong>, myarray));
    331 </pre>
    332 
    333 <p>Trong v d ny, {@link android.R.layout#simple_list_item_1} l mt ti nguyn b tr c nh ngha bi
    334 nn tng cho cc mc trong mt {@link android.widget.ListView}. Bn c th s dng iu ny thay v to
    335 b tr ring ca mnh cho cc mc danh sch.  bit thm thng tin, hy xem phn
    336 <a href="{@docRoot}guide/topics/ui/layout/listview.html">Dng xem Danh sch</a> trong hng dn cho nh pht trin.</p>
    337 
    338