Home | History | Annotate | Download | only in ui
      1 page.title=
      2 page.tags=view,viewgroup
      3 @jd:body
      4 
      5 <div id="qv-wrapper">
      6 <div id="qv">
      7   <h2> </h2>
      8 <ol>
      9   <li><a href="#write"> XML</a></li>
     10   <li><a href="#load">  XML</a></li>
     11   <li><a href="#attributes"></a>
     12     <ol>
     13       <li><a href="#id"></a></li>
     14       <li><a href="#layout-params"> </a></li>
     15     </ol>
     16   </li>
     17   <li><a href="#Position"> </a></li>
     18   <li><a href="#SizePaddingMargins">,   </a></li>
     19   <li><a href="#CommonLayouts">  </a></li>
     20   <li><a href="#AdapterViews">    </a>
     21     <ol>
     22       <li><a href="#FillingTheLayout">   </a></li>
     23       <li><a href="#HandlingUserSelections"> </a></li>
     24     </ol>
     25   </li>
     26 </ol>
     27 
     28   <h2> </h2>
     29   <ol>
     30     <li>{@link android.view.View}</li>
     31     <li>{@link android.view.ViewGroup}</li>
     32     <li>{@link android.view.ViewGroup.LayoutParams}</li>
     33   </ol>
     34 
     35   <h2>. :</h2>
     36   <ol>
     37     <li><a href="{@docRoot}training/basics/firstapp/building-ui.html"> 
     38  </a></li> </div>
     39 </div>
     40 
     41 <p>     , ,   <a href="{@docRoot}guide/components/activities.html"></a>  <a href="{@docRoot}guide/topics/appwidgets/index.html"> </a>.
     42     :</p>
     43 <ul>
     44 <li><strong>     XML</strong>.  Android    XML-
     45   View   ,  ,      .</li>
     46 <li><strong>     </strong>. 
     47       View  ViewGroup (    ). </li>
     48 </ul>
     49 
     50 <p> Android                 . ,     XML   ,   ,     ,   .       ,        (   XML)   . </p>
     51 
     52 <div class="sidebox-wrapper">
     53 <div class="sidebox">
     54   <ul>
     55   <li> <a href="{@docRoot}tools/sdk/eclipse-adt.html">  ADT
     56  Eclipse</a>        XML&mdash;
     57    XML    <strong>Layout</strong> ().</li>
     58   <li>     
     59 <a href="{@docRoot}tools/debugging/debugging-ui.html#hierarchyViewer">Hierarchy Viewer</a>
     60 &mdash;       ,
     61      ,     
     62          .</li>
     63   <li>   <a href="{@docRoot}tools/debugging/debugging-ui.html#layoutopt">layoutopt</a>
     64              .</li>
     65 </div>
     66 </div>
     67 
     68 <p>      XML   ,             ,    .         .  ,                  . ,     XML         ,     .  ,    XML     ,       .          XML.  
     69     View   ,       {@link android.view.ViewGroup} 
     70 {@link android.view.View}.</p>
     71 
     72 <p> ,  XML-                    ,     . ,    ,      ,   XML      ,       XML.   ,      .       . ,
     73   EditText   <code>text</code>,   
     74 <code>EditText.setText()</code>. </p>
     75 
     76 <p class="note"><strong>.</strong>         
     77 <a href="{@docRoot}guide/topics/ui/layout-objects.html">  </a>.     
     78 <a href="{@docRoot}resources/tutorials/views/index.html">  Hello </a>      .</p>
     79 
     80 <h2 id="write"> XML</h2>
     81 
     82 <p>   XML-,    Android,             ,   ,    -  HTML&mdash;    . </p>
     83 
     84 <p>         ,        (View)    (ViewGroup).                      ,    .     XML,      {@link android.widget.LinearLayout},
     85     {@link android.widget.TextView}  {@link android.widget.Button}.</p>
     86 <pre>
     87 &lt;?xml version="1.0" encoding="utf-8"?>
     88 &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     89               android:layout_width="match_parent"
     90               android:layout_height="match_parent"
     91               android:orientation="vertical" >
     92     &lt;TextView android:id="@+id/text"
     93               android:layout_width="wrap_content"
     94               android:layout_height="wrap_content"
     95               android:text="Hello, I am a TextView" />
     96     &lt;Button android:id="@+id/button"
     97             android:layout_width="wrap_content"
     98             android:layout_height="wrap_content"
     99             android:text="Hello, I am a Button" />
    100 &lt;/LinearLayout>
    101 </pre>
    102 
    103 <p>     XML     <code>.xml</code>
    104   <code>res/layout/</code>   Android   . </p>
    105 
    106 <p>      XML     <a href="{@docRoot}guide/topics/resources/layout-resource.html"> </a>.</p>
    107 
    108 <h2 id="load">  XML</h2>
    109 
    110 <p>      XML    
    111 {@link android.view.View}.              
    112 {@link android.app.Activity#onCreate(android.os.Bundle) Activity.onCreate()}.
    113    
    114 <code>{@link android.app.Activity#setContentView(int) setContentView()}</code>,          :
    115 <code>R.layout.<em>layout_file_name</em></code>.
    116 ,   XML    <code>main_layout.xml</code>,  
    117      :</p>
    118 <pre>
    119 public void onCreate(Bundle savedInstanceState) {
    120     super.onCreate(savedInstanceState);
    121     setContentView(R.layout.main_layout);
    122 }
    123 </pre>
    124 
    125 <p>   <code>onCreate()</code>      Android  
    126   (.     
    127 
    128 <a href="{@docRoot}guide/components/activities.html#Lifecycle"></a>).</p>
    129 
    130 
    131 <h2 id="attributes"></h2>
    132 
    133 <p>  View  ViewGroup     XML.
    134       View (,  TextView   <code>textSize</code>),
    135        View,     .
    136        View,       View (  
    137 <code>id</code>).       .  
    138       View,  
    139   ViewGroup  .</p>
    140 
    141 <h3 id="id"></h3>
    142 
    143 <p>   View       ,       View  .
    144          ,  
    145     XML       <code>id</code>.
    146   XML      View
    147 (  {@link android.view.View}),      .
    148      XML :</p>
    149 <pre>android:id="&#64;+id/my_button"</pre>
    150 
    151 <p> @      ,   XML      
    152 ,           .   (+) ,     ,
    153         (  <code>R.java</code>).  Android    
    154 .      Android      ,
    155       <code>android</code>,   :</p>
    156 <pre>android:id="&#64;android:id/empty"</pre>
    157 <p>     <code>android</code>       <code>android.R</code>,
    158      .</p>
    159 
    160 <p>        ,      .</p>
    161 <ol>
    162   <li>           :
    163 <pre>
    164 &lt;Button android:id="&#64;+id/my_button"
    165         android:layout_width="wrap_content"
    166         android:layout_height="wrap_content"
    167         android:text="&#64;string/my_button_text"/>
    168 </pre>
    169   </li>
    170   <li>          
    171 (    <code>{@link android.app.Activity#onCreate(Bundle) onCreate()}</code>):
    172 <pre>
    173 Button myButton = (Button) findViewById(R.id.my_button);
    174 </pre>
    175   </li>
    176 </ol>
    177 <p>           {@link android.widget.RelativeLayout}.
    178       
    179     .</p>
    180 <p>         ,
    181      ,     (       , 
    182       ).</p>
    183 
    184 
    185 <h3 id="layout-params"> </h3>
    186 
    187 <p>  XML,   <code>layout_<em>something</em></code>, 
    188     ,    ViewGroup,    .</p>
    189 
    190 <p>  ViewGroup   ,   {@link
    191 android.view.ViewGroup.LayoutParams}.   
    192   ,        ,
    193    .  1 ,   
    194         (   ).</p>
    195 
    196 <img src="{@docRoot}images/layoutparams.png" alt="" />
    197 <p class="img-caption"><strong>1.</strong>      
    198   .</p>
    199 
    200 <p> ,   LayoutParams     
    201 .      LayoutParams,      ,
    202        LayoutParams    . </p>
    203 
    204 <p>          (<code>layout_width</code> 
    205 <code>layout_height</code>),      . 
    206 LayoutParams       . <p>
    207 
    208 <p>        , , ,
    209      .           
    210 : </p>
    211 
    212 <ul>
    213   <li><var>wrap_content</var>      
    214  ;</li>
    215   <li><var>match_parent</var> (  API  8 <var> fill_parent</var> )
    216      ,     .</li>
    217 </ul>
    218 
    219 <p> ,         
    220 (,  ).      ,  
    221 ,      (<var>dp</var>), <var>wrap_content</var>
    222 <var>match_parent</var>   
    223        .
    224     
    225 
    226 <a href="{@docRoot}guide/topics/resources/available-resources.html#dimension"> </a>.</p>
    227 
    228 
    229 <h2 id="Position"> </h2>
    230    <p>
    231       .  
    232    <em></em>  <em></em>,   
    233     .  
    234  .
    235    </p>
    236 
    237    <p>
    238          
    239 {@link android.view.View#getLeft()}  {@link android.view.View#getTop()}.     (  X)
    240   .    
    241  (  Y)   .   
    242       . ,
    243   <code>getLeft()</code>  20,  ,      20
    244       .
    245    </p>
    246 
    247    <p>
    248     ,    
    249 ({@link android.view.View#getRight()}  {@link android.view.View#getBottom()}),     .
    250           
    251  . ,   {@link android.view.View#getRight()}
    252   : <code>getLeft() + getWidth()</code>.
    253    </p>
    254 
    255 
    256 <h2 id="SizePaddingMargins">,   </h2>
    257    <p>
    258          . , 
    259     -.
    260    </p>
    261 
    262    <p>
    263       <em> </em> 
    264 <em> </em>.     
    265     . 
    266   ,   {@link android.view.View#getMeasuredWidth()}
    267 {@link android.view.View#getMeasuredHeight()}.
    268    </p>
    269 
    270    <p>
    271         <em></em>  <em></em> (  
    272 <em> </em>  <em> </em>).  
    273           
    274 .     
    275    ,     .      ,  
    276 {@link android.view.View#getWidth()}  {@link android.view.View#getHeight()}.
    277    </p>
    278 
    279    <p>
    280          . 
    281     , ,     .
    282             
    283 . ,   ,  2,   ,     
    284  2     .      
    285 {@link android.view.View#setPadding(int, int, int, int)}.   ,  
    286 {@link android.view.View#getPaddingLeft()}, {@link android.view.View#getPaddingTop()},
    287 {@link android.view.View#getPaddingRight()}  {@link android.view.View#getPaddingBottom()}.
    288    </p>
    289 
    290    <p>
    291         ,    
    292 .      .       
    293 {@link android.view.ViewGroup} 
    294 {@link android.view.ViewGroup.MarginLayoutParams}.
    295    </p>
    296 
    297    <p>      
    298 <a href="{@docRoot}guide/topics/resources/more-resources.html#Dimension"> </a>.
    299    </p>
    300 
    301 
    302 
    303 
    304 
    305 
    306 <style type="text/css">
    307 div.layout {
    308   float:left;
    309   width:200px;
    310   margin:0 0 20px 20px;
    311 }
    312 div.layout.first {
    313   margin-left:0;
    314   clear:left;
    315 }
    316 </style>
    317 
    318 
    319 
    320 
    321 <h2 id="CommonLayouts">  </h2>
    322 
    323 <p>    {@link android.view.ViewGroup}    
    324    .         ,     
    325 Android.</p>
    326 
    327 <p class="note"><strong>.</strong>   ,      
    328        ,      
    329  .      ,      ( 
    330    ).</p>
    331 
    332 <!--
    333 <h2 id="framelayout">FrameLayout</h2>
    334 <p>{@link android.widget.FrameLayout FrameLayout} is the simplest type of layout
    335 object. It's basically a blank space on your screen that you can
    336 later fill with a single object &mdash; for example, a picture that you'll swap in and out.
    337 All child elements of the FrameLayout are pinned to the top left corner of the screen; you cannot
    338 specify a different location for a child view. Subsequent child views will simply be drawn over
    339 previous ones,
    340 partially or totally obscuring them (unless the newer object is transparent).
    341 </p>
    342 -->
    343 
    344 
    345 <div class="layout first">
    346   <h4><a href="layout/linear.html"> </a></h4>
    347   <a href="layout/linear.html"><img src="{@docRoot}images/ui/linearlayout-small.png" alt="" /></a>
    348   <p>,          .      ,  
    349   .</p>
    350 </div>
    351 
    352 <div class="layout">
    353   <h4><a href="layout/relative.html"> </a></h4>
    354   <a href="layout/relative.html"><img src="{@docRoot}images/ui/relativelayout-small.png" alt="" /></a>
    355   <p>           (  
    356    )     (     ).</p>
    357 </div>
    358 
    359 <div class="layout">
    360   <h4><a href="{@docRoot}guide/webapps/webview.html"> -</a></h4>
    361   <a href="{@docRoot}guide/webapps/webview.html"><img src="{@docRoot}images/ui/webview-small.png" alt="" /></a>
    362   <p> -.</p>
    363 </div>
    364 
    365 
    366 
    367 
    368 <h2 id="AdapterViews" style="clear:left">    </h2>
    369 
    370 <p>        ,   ,    
    371 {@link android.widget.AdapterView}       .  
    372 {@link android.widget.AdapterView}  {@link android.widget.Adapter} 
    373     . {@link android.widget.Adapter}      
    374    {@link android.widget.AdapterView}&mdash; {@link android.widget.Adapter}
    375   ( , ,      )    
    376  ,      {@link android.widget.AdapterView}.</p>
    377 
    378 <p>  ,      ,    :</p>
    379 
    380 <div class="layout first">
    381   <h4><a href="layout/listview.html">   </a></h4>
    382   <a href="layout/listview.html"><img src="{@docRoot}images/ui/listview-small.png" alt="" /></a>
    383   <p>       .</p>
    384 </div>
    385 
    386 <div class="layout">
    387   <h4><a href="layout/gridview.html">   </a></h4>
    388   <a href="layout/gridview.html"><img src="{@docRoot}images/ui/gridview-small.png" alt="" /></a>
    389   <p>        .</p>
    390 </div>
    391 
    392 
    393 
    394 <h3 id="FillingTheLayout" style="clear:left">   </h3>
    395 
    396 <p>   {@link android.widget.AdapterView},   {@link android.widget.ListView} 
    397 {@link android.widget.GridView},   {@link android.widget.AdapterView}  
    398 {@link android.widget.Adapter},          {@link
    399 android.view.View},     .</p>
    400 
    401 <p> Android     {@link android.widget.Adapter},    
    402         {@link android.widget.AdapterView}. 
    403     :</p>
    404 
    405 <dl>
    406   <dt>{@link android.widget.ArrayAdapter}</dt>
    407     <dd>    ,       .   {@link
    408 android.widget.ArrayAdapter}          {@link
    409 java.lang.Object#toString()}          {@link
    410 android.widget.TextView}.
    411       <p>,    ,      {@link
    412 android.widget.ListView},   {@link android.widget.ArrayAdapter}  
    413 ,         :</p>
    414 <pre>
    415 ArrayAdapter&lt;String> adapter = new ArrayAdapter&lt;String>(this,
    416         android.R.layout.simple_list_item_1, myStringArray);
    417 </pre>
    418 <p>    :</p>
    419 <ul>
    420   <li>  {@link android.content.Context};</li>
    421   <li>,     {@link android.widget.TextView}     ;</li>
    422   <li> .</li>
    423 </ul>
    424 <p>   
    425 {@link android.widget.ListView#setAdapter setAdapter()}    {@link android.widget.ListView}:</p>
    426 <pre>
    427 ListView listView = (ListView) findViewById(R.id.listview);
    428 listView.setAdapter(adapter);
    429 </pre>
    430 
    431       <p>     ,    {@link
    432 java.lang.Object#toString()}    .       ,
    433   {@link android.widget.TextView} (,       
    434 {@link android.widget.ImageView}),   {@link
    435 android.widget.ArrayAdapter}    {@link android.widget.ArrayAdapter#getView
    436 getView()},        .</p>
    437 
    438 </dd>
    439 
    440   <dt>{@link android.widget.SimpleCursorAdapter}</dt>
    441     <dd>    ,        {@link android.database.Cursor}. 
    442  {@link android.widget.SimpleCursorAdapter},   ,  
    443       {@link android.database.Cursor},            {@link android.database.Cursor}
    444 . ,    
    445      ,   ,    {@link
    446 android.database.Cursor},           
    447 .          {@link
    448 android.database.Cursor},        ,     ,   
    449      :</p>
    450 <pre>
    451 String[] fromColumns = {ContactsContract.Data.DISPLAY_NAME,
    452                         ContactsContract.CommonDataKinds.Phone.NUMBER};
    453 int[] toViews = {R.id.display_name, R.id.phone_number};
    454 </pre>
    455 <p>    {@link android.widget.SimpleCursorAdapter}    ,
    456      ,  {@link android.database.Cursor}      :</p>
    457 <pre>
    458 SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
    459         R.layout.person_name_and_number, cursor, fromColumns, toViews, 0);
    460 ListView listView = getListView();
    461 listView.setAdapter(adapter);
    462 </pre>
    463 <p> {@link android.widget.SimpleCursorAdapter}       
    464 {@link android.database.Cursor}         {@code
    465 fromColumns}    {@code toViews}.</p>.</dd>
    466 </dl>
    467 
    468 
    469 <p>            ,  
    470  ,     {@link android.widget.ArrayAdapter#notifyDataSetChanged()}.  
    471     ,         .</p>
    472 
    473 
    474 
    475 <h3 id="HandlingUserSelections"> </h3>
    476 
    477 <p>        {@link android.widget.AdapterView},
    478   {@link android.widget.AdapterView.OnItemClickListener}. :</p>
    479 
    480 <pre>
    481 // Create a message handling object as an anonymous class.
    482 private OnItemClickListener mMessageClickedHandler = new OnItemClickListener() {
    483     public void onItemClick(AdapterView parent, View v, int position, long id) {
    484         // Do something in response to the click
    485     }
    486 };
    487 
    488 listView.setOnItemClickListener(mMessageClickedHandler);
    489 </pre>
    490 
    491 
    492 
    493