Home | History | Annotate | Download | only in resources
      1 page.title=  
      2 parent.title= 
      3 parent.link=index.html
      4 @jd:body
      5 
      6 <div id="qv-wrapper">
      7 <div id="qv">
      8   <h2> </h2>
      9   <ul>
     10     <li>            {@code R.java},  
     11 {@code R.drawable.myimage}</li>
     12     <li>            XML,  {@code
     13 &#64;drawable/myimage}</li>
     14     <li>        
     15 {@link android.content.res.Resources}</li>
     16   </ul>
     17 
     18   <h2> </h2>
     19   <ol>
     20     <li>{@link android.content.res.Resources}</li>
     21   </ol>
     22 
     23   <h2> </h2>
     24   <ol>
     25     <li><a href="#ResourcesFromCode">    </a></li>
     26     <li><a href="#ResourcesFromXml">    XML</a>
     27       <ol>
     28         <li><a href="#ReferencesToThemeAttributes">   </a></li>
     29       </ol>
     30     </li>
     31     <li><a href="#PlatformResources">   </a></li>
     32   </ol>
     33 
     34   <h2>. :</h2>
     35   <ol>
     36     <li><a href="providing-resources.html"> </a></li>
     37     <li><a href="available-resources.html"> </a></li>
     38   </ol>
     39 </div>
     40 </div>
     41 
     42 
     43 
     44 
     45 <p> ,        (.  <a href="providing-resources.html"> </a>),    .    
     46    .           {@code R},    
     47 {@code aapt}.</p>
     48 
     49 <p>     {@code aapt}   {@code R},   
     50       {@code
     51 res/}.       {@code R} (,
     52  {@code R.drawable}   ),        
     53   (, {@code R.drawable.icon}).        ,  
     54    .</p>
     55 
     56 <p>  ,    {@code R}   ,     
     57    .     :</p>
     58 <ul>
     59   <li><em> </em>:    ,   {@code
     60 string}, {@code drawable}  {@code layout}.         <a href="available-resources.html"> </a>.
     61   </li>
     62   <li><em> </em>,       
     63 ( ),    XML {@code android:name} (
     64     ,  ).</li>
     65 </ul>
     66 
     67 <p>     .</p>
     68 <ul>
     69   <li><strong> :</strong>          {@code R}
     70 , :
     71     <pre class="classic no-pretty-print">R.string.hello</pre>
     72     <p>{@code string}   ,  {@code hello}   .   API-
     73 Android,      ,      . . 
     74 <a href="#ResourcesFromCode">    </a>.</p>
     75   </li>
     76   <li><strong> XML:</strong>     XML,   
     77  ,    {@code R}, :
     78     <pre class="classic no-pretty-print">&#64;string/hello</pre>
     79     <p>{@code string}   ,  {@code hello}   .   
     80      XML,   ,    . .  <a href="#ResourcesFromXml">    XML</a></p>
     81   </li>
     82 </ul>
     83 
     84 
     85 
     86 <h2 id="ResourcesFromCode">     </h2>
     87 
     88 <p>    ,        . , 
     89   {@link android.widget.ImageView#setImageResource(int) setImageResource()}     {@link android.widget.ImageView}  {@code res/drawable/myimage.png}:
     90 </p>
     91 <pre>
     92 ImageView imageView = (ImageView) findViewById(R.id.myimageview);
     93 imageView.setImageResource(<strong>R.drawable.myimage</strong>);
     94 </pre>
     95 
     96 <p>          {@link
     97 android.content.res.Resources},      
     98  {@link android.content.Context#getResources()}.</p>
     99 
    100 <div class="sidebox-wrapper">
    101 <div class="sidebox">
    102 <h2>   </h2>
    103 
    104 <p>           .     
    105    {@code res/}  ,      
    106 {@code res/}     .       
    107 {@code assets/}.</p>
    108 <p>,     {@code assets/}, <em></em>  
    109 ,           {@code R}    XML.   
    110     {@code assets/},     ,      
    111 {@link android.content.res.AssetManager}.</p>
    112 <p>,          (, -  ),
    113       {@code res/raw/}        {@link
    114 android.content.res.Resources#openRawResource(int) openRawResource()}.</p>
    115 
    116 </div>
    117 </div>
    118 
    119 
    120 <h3></h3>
    121 
    122 <p>       .</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>   ,     (
    130          ).</li>
    131   <li><em>{@code &lt;resource_type&gt;}</em>   {@code R}   .</li>
    132   <li><em>{@code &lt;resource_name&gt;}</em>    
    133  ( ),    {@code android:name}   XML ( 
    134 ).</li>
    135 </ul>
    136 <p>    
    137        .   <a href="available-resources.html"> </a>.</p>
    138 
    139 
    140 <h3> </h3>
    141 
    142 <p>  ,        .      ,
    143    {@link android.content.res.Resources}.    {@link
    144 android.content.res.Resources}   {@link android.content.Context#getResources
    145 Context.getResources()}.</p>
    146 
    147 
    148 <p>       :</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>!</strong>     {@code
    179 R.java} &mdash;     {@code aapt}    
    180 .         .</p>
    181 
    182 
    183 
    184 <h2 id="ResourcesFromXml">    XML</h2>
    185 
    186 <p>        XML  
    187    .        
    188      .</p>
    189 
    190 <p>,      {@link android.widget.Button}  
    191 <a href="string-resource.html"> </a>    :</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></h3>
    202 
    203 <p>        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;}   ,     (
    211            ).</li>
    212   <li>{@code &lt;resource_type&gt;}   
    213 {@code R}   .</li>
    214   <li>{@code &lt;resource_name&gt;}    
    215  ( ),    {@code android:name}   XML ( 
    216 ).</li>
    217 </ul>
    218 
    219 <p>    
    220        .   <a href="available-resources.html"> </a>.</p>
    221 
    222 
    223 <h3> </h3>
    224 
    225 <p>           XML (,      
    226 ),          XML,    . ,
    227     ,  <a href="more-resources.html#Color"> </a>  <a href="string-resource.html"> </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>            
    238 :</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>           , 
    250      .   
    251         . :</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>.</strong>    ,
    263         . 
    264    
    265  (,  )    <a href="providing-resources.html#AlternativeResources"> 
    266 </a>.   <a href="localization.html"></a>     
    267 .</p>
    268 
    269 <p>      XML   . ,   
    270  ,        :</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>   ,         .   
    279 <a href="providing-resources.html#AliasResources">  </a>.</p>
    280 
    281 
    282 
    283 <h3 id="ReferencesToThemeAttributes">   </h3>
    284 
    285 <p>         
    286     .   
    287      ,       
    288  ,     .     
    289      ,      .</p>
    290 
    291 <p>          
    292 ,      {@code @}     ({@code ?}),   
    293   . :</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>             
    300      :</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>  {@code android:textColor}      
    311   .   Android  ,    {@code android:textColorSecondary}
    312     {@code android:textColor}   .      
    313  ,       ,
    314        (  
    315 <code>?android:attr/textColorSecondary</code>)&mdash;  {@code attr}  .</p>
    316 
    317 
    318 
    319 
    320 <h2 id="PlatformResources">   </h2>
    321 
    322 <p> Android    , , ,   . 
    323           
    324 <code>android</code>. ,  Android   ,    
    325     {@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>   {@link android.R.layout#simple_list_item_1}    , 
    334      {@link android.widget.ListView}.      
    335     .       , 
    336 <a href="{@docRoot}guide/topics/ui/layout/listview.html">   </a>.</p>
    337 
    338