Home | History | Annotate | Download | only in ui
      1 page.title=B tr
      2 page.tags=dng xem, nhm dng xem
      3 @jd:body
      4 
      5 <div id="qv-wrapper">
      6 <div id="qv">
      7   <h2>Trong ti liu ny</h2>
      8 <ol>
      9   <li><a href="#write">Ghi XML</a></li>
     10   <li><a href="#load">Np Ti nguyn XML</a></li>
     11   <li><a href="#attributes">Thuc tnh</a>
     12     <ol>
     13       <li><a href="#id">ID</a></li>
     14       <li><a href="#layout-params">Tham s B tr</a></li>
     15     </ol>
     16   </li>
     17   <li><a href="#Position">V tr B tr</a></li>
     18   <li><a href="#SizePaddingMargins">Kch c, Phn m v L</a></li>
     19   <li><a href="#CommonLayouts">Cc B tr Thng gp</a></li>
     20   <li><a href="#AdapterViews">Xy dng B tr bng mt Trnh iu hp</a>
     21     <ol>
     22       <li><a href="#FillingTheLayout">in d liu vo mt dng xem trnh iu hp</a></li>
     23       <li><a href="#HandlingUserSelections">X l s kin nhp</a></li>
     24     </ol>
     25   </li>
     26 </ol>
     27 
     28   <h2>Lp kha</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>Xem thm</h2>
     36   <ol>
     37     <li><a href="{@docRoot}training/basics/firstapp/building-ui.html">Xy dng mt Giao din Ngi dng
     38 n gin</a></li> </div>
     39 </div>
     40 
     41 <p>B tr nh ngha cu trc hin th cho mt giao din ngi dng, chng hn nh UI cho mt <a href="{@docRoot}guide/components/activities.html">hot ng</a> hoc <a href="{@docRoot}guide/topics/appwidgets/index.html">widget ng dng</a>.
     42 Bn c th khai bo mt b tr bng hai cch:</p>
     43 <ul>
     44 <li><strong>Khai bo phn t UI trong XML</strong>. Android cung cp mt kho t vng XML
     45 n gin, tng ng vi cc lp v lp con Dng xem, chng hn nh dnh cho cc widget v b tr.</li>
     46 <li><strong>Khi to cc phn t b tr vo thi gian chy</strong>. ng dng
     47 ca bn c th to cc i tng Dng xem v Nhm Dng xem (v thao tc trn cc tnh cht ca n) theo lp trnh. </li>
     48 </ul>
     49 
     50 <p>Khun kh Android cho bn s linh hot trong khi s dng mt hoc c hai phng php ny  khai bo v qun l UI ng dng ca mnh. V d, bn c th khai bo cc b tr mc nh cho ng dng ca mnh trong XML, bao gm cc phn t mn hnh m s xut hin trong chng hoc tnh cht ca chng. Sau , bn c th thm m trong ng dng ca mnh  sa i trng thi ca cc i tng trn mn hnh, bao gm nhng i tng c khai bo trong XML, vo thi gian chy. </p>
     51 
     52 <div class="sidebox-wrapper">
     53 <div class="sidebox">
     54   <ul>
     55   <li>Phn b tr <a href="{@docRoot}tools/sdk/eclipse-adt.html">ADT
     56  cho Eclipse</a> s a ra mt bn xem trc b tr XML ca bn &mdash;
     57   c tp XML c m, hy chn tab <strong>B tr</strong>.</li>
     58   <li>Bn cng nn th cng c
     59   <a href="{@docRoot}tools/debugging/debugging-ui.html#hierarchyViewer">Trnh xem Phn cp</a>,
     60    g ri cc b tr &mdash; n h l cc gi tr tnh cht ca b tr,
     61   v ng vin bng cc ch bo phn m/l, v cc dng xem c dng y  trong khi
     62   bn g ri trn trnh m phng hoc thit b.</li>
     63   <li>Cng c <a href="{@docRoot}tools/debugging/debugging-ui.html#layoutopt">layoutopt</a> cho php
     64   bn nhanh chng phn tch cc b tr v phn cp ca mnh xem c khng hiu qu hoc c cc vn  khc khng.</li>
     65 </div>
     66 </div>
     67 
     68 <p>u im ca vic khai bo UI ca bn trong XML l cho php bn tch vic trnh by ng dng ca mnh vi m iu khin hnh vi ca n hiu qu hn. M t UI ca bn nm ngoi m ng dng ca bn, iu ny c ngha rng bn c th sa i hoc iu hp n m khng phi sa i m ngun ca bn v bin dch li. V d, bn c th to b tr XML cho cc hng mn hnh khc nhau, kch c mn hnh khc nhau, v ngn ng khc nhau. Ngoi ra, vic khai bo b tr trong XML gip d dng hn trong vic hin th cu trc UI ca bn, v vy s d g li s c hn. Nh vy, ti liu ny tp trung vo vic hng dn bn cch khai bo b tr ca mnh trong XML. Nu bn
     69 quan tm ti vic khi to cc i tng Dng xem vo thi gian chy, hy tham kho {@link android.view.ViewGroup} v
     70 ti liu tham kho lp {@link android.view.View}.</p>
     71 
     72 <p>Nhn chung, kho t vng ca XML i vi vic khai bo cc phn t UI tun th cht ch cu trc v cch t tn cc lp v phng php, trong  cc tn phn t tng ng vi tn lp v tn thuc tnh tng ng vi phng php. Trn thc t, s tng ng thng trc tip n mc bn c th on thuc tnh XML no tng ng vi mt phng php lp, hoc on xem lp no tng ng vi mt phn t XML cho trc. Tuy nhin, lu  rng khng phi tt c t vng u ging nhau. Trong mt s trng hp, c s khc bit nh trong vic t tn. V
     73 d, phn t EditText c thuc tnh <code>text</code> tng ng vi
     74 <code>EditText.setText()</code>. </p>
     75 
     76 <p class="note"><strong>Mo:</strong> Tm hiu thm v cc kiu b tr trong <a href="{@docRoot}guide/topics/ui/layout-objects.html">i tng B tr
     77 Thng gp</a>. C mt tuyn tp cc bi hng dn v vic xy dng cc b tr khc nhau trong hng dn bi hc
     78 <a href="{@docRoot}resources/tutorials/views/index.html">Dng xem Hello</a>.</p>
     79 
     80 <h2 id="write">Ghi XML</h2>
     81 
     82 <p>Khi s dng t vng XML ca Android, bn c th nhanh chng thit k cc b tr UI v phn t mn hnh m chng cha, ging nh cch bn to trang web trong HTML &mdash; bng mt lot cc phn t lng nhau. </p>
     83 
     84 <p>Mi tp b tr phi cha chnh xc mt phn t gc,  phi l mt i tng Dng xem hoc Nhm Dng xem. Sau khi  nh ngha phn t gc, bn c th thm cc i tng hoc widget b tr b sung lm phn t con  dn dn xy dng mt phn cp Dng xem nh ngha b tr ca bn. V d, sau y l mt b tr XML s dng mt {@link android.widget.LinearLayout}
     85 thng ng  gi mt {@link android.widget.TextView} v mt {@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>Sau khi bn  khai bo b tr ca mnh trong XML, hy lu tp vi phn m rng <code>.xml</code>,
    104 trong th mc d n <code>res/layout/</code> Android ca bn  bin dch cho ph hp. </p>
    105 
    106 <p>Thng tin v c php i vi tp XML b tr c sn trong ti liu <a href="{@docRoot}guide/topics/resources/layout-resource.html">Ti nguyn B tr</a>.</p>
    107 
    108 <h2 id="load">Np Ti nguyn XML</h2>
    109 
    110 <p>Khi bn bin dch ng dng ca mnh, tng tp b tr XML c bin dch thnh mt ti nguyn
    111 {@link android.view.View}. Bn nn np ti nguyn b tr t m ng dng ca mnh, trong trin khai gi li
    112 {@link android.app.Activity#onCreate(android.os.Bundle) Activity.onCreate()} ca bn.
    113 Lm vy bng cch gi <code>{@link android.app.Activity#setContentView(int) setContentView()}</code>,
    114 chuyn cho n tham chiu ti ti nguyn b tr ca bn di hnh thc:
    115 <code>R.layout.<em>layout_file_name</em></code>.
    116 V d, nu b tr XML ca bn c lu thnh <code>main_layout.xml</code>, bn s np n
    117 cho Hot ng ca mnh nh sau:</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>Phng php gi li <code>onCreate()</code> trong Hot ng ca bn c gi bi khun kh Android khi
    126 Hot ng ca bn c khi chy (xem phn tho lun v vng i, trong ti liu
    127 <a href="{@docRoot}guide/components/activities.html#Lifecycle">Hot ng</a>
    128 ).</p>
    129 
    130 
    131 <h2 id="attributes">Thuc tnh</h2>
    132 
    133 <p>Mi i tng Dng xem v Nhm Dng xem u h tr cc phin bn thuc tnh XML ca chnh mnh.
    134 Mt s thuc tnh p dng ring cho i tng Dng xem (v d, TextView h tr thuc tnh <code>textSize</code>
    135 ), nhng nhng thuc tnh ny cng c k tha bi bt k i tng Dng xem no m c th m rng lp ny.
    136 Mt s c p dng chung cho tt c i tng Dng xem v chng c k tha t lp Dng xem gc (nh
    137 thuc tnh <code>id</code>). V nhng thuc tnh cn li c coi l "tham s b tr,"  l nhng thuc tnh
    138 m t mt s hng b tr nht nh ca i tng Dng xem, nh c nh ngha bi i tng Nhm Dng xem
    139 m ca i tng .</p>
    140 
    141 <h3 id="id">ID</h3>
    142 
    143 <p>Bt k i tng Dng xem no cng c mt ID s nguyn c lin kt vi n  nhn bit duy nht Dng xem trong cy.
    144 Khi ng dng c bin dch, ID ny c tham chiu nh mt s nguyn, nhng ID thng
    145 c gn trong tp XML b tr nh mt xu, trong thuc tnh <code>id</code>.
    146 y l thuc tnh XML chung cho tt c i tng Dng xem
    147 (c nh ngha theo lp {@link android.view.View}) v bn s rt hay s dng n.
    148 C php i vi mt ID bn trong mt tag XML l:</p>
    149 <pre>android:id="&#64;+id/my_button"</pre>
    150 
    151 <p>Biu tng "a mc" (@)  u xu th hin rng trnh phn tch XML nn phn tch v m rng phn cn li
    152 ca xu ID v nhn bit n nh mt ti nguyn ID. Biu tng du cng (+) c ngha rng y l mt tn ti nguyn mi m phi
    153 c to v thm vo ti nguyn ca chng ta (trong tp <code>R.java</code>). C nhiu ti nguyn ID khc
    154 c cung cp bi khun kh Android. Khi tham chiu mt ID ti nguyn Android, bn khng cn biu tng du cng,
    155 nhng phi thm vng tn gi <code>android</code>, nh sau:</p>
    156 <pre>android:id="&#64;android:id/empty"</pre>
    157 <p>Khi  c vng tn gi <code>android</code>, gi chng ta ang tham chiu mt ID t lp ti nguyn <code>android.R</code>
    158 , thay v lp ti nguyn cc b.</p>
    159 
    160 <p> to cc dng xem v tham chiu chng t ng dng, mt m thc thng thy  l:</p>
    161 <ol>
    162   <li>nh ngha mt dng xem/widget trong tp b tr v gn cho n mt ID duy nht:
    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>Sau , to mt thc th ca i tng dng xem v chp n t b tr
    171 (thng trong phng php <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>nh ngha cc ID cho i tng dng xem l vic quan trng khi to mt {@link android.widget.RelativeLayout}.
    178 Trong mt b tr tng i, cc dng xem ng hng c th nh ngha b tr ca n so vi dng xem ng hng kia,
    179 dng xem m c tham chiu bi ID duy nht.</p>
    180 <p>Mt ID khng cn phi l duy nht trong ton b cy, nhng nn l
    181 duy nht trong b phn ca cy m bn ang tm kim (thng l ton b cy, v th tt nht l
    182 ID nn hon ton duy nht khi c th).</p>
    183 
    184 
    185 <h3 id="layout-params">Tham s B tr</h3>
    186 
    187 <p>Cc thuc tnh b tr XML <code>layout_<em>something</em></code> s nh ngha
    188 cc tham s b tr cho Dng xem ph hp vi Nhm Dng xem m n nm trong .</p>
    189 
    190 <p>Mi lp Nhm Dng xem u trin khai mt lp lng nhau c chc nng m rng {@link
    191 android.view.ViewGroup.LayoutParams}. Lp con ny
    192 cha cc kiu tnh cht m nh ngha kch c v v tr ca tng dng xem con cho
    193 ph hp vi nhm dng xem. Nh bn c th thy trong hnh 1, nhm dng xem
    194 m s nh ngha cc tham s b tr cho tng dng xem con (bao gm nhm dng xem con).</p>
    195 
    196 <img src="{@docRoot}images/layoutparams.png" alt="" />
    197 <p class="img-caption"><strong>Hnh 1.</strong> Trc quan ha mt phn cp dng xem vi cc tham s
    198 b tr c lin kt vi tng dng xem.</p>
    199 
    200 <p>  rng mi lp con LayoutParams u c c php ring ca mnh cho cc gi tr
    201 thit t. Mi phn t con phi nh ngha LayoutParams cho ph hp vi phn t m ca n,
    202 mc d cng c th nh ngha LayoutParams khc cho phn t con ca chnh n. </p>
    203 
    204 <p>Tt c nhm dng xem u c chiu rng v chiu cao (<code>layout_width</code> v
    205 <code>layout_height</code>), v mi dng xem u phi nh ngha chng. Nhiu
    206 LayoutParams cng c l v vin ty chn. <p>
    207 
    208 <p>Bn c th ch nh chiu rng v chiu cao bng cc s o chnh xc, mc d c th
    209 bn s khng mun lm iu ny thng xuyn. Bn s thng s dng mt trong nhng hng s ny 
    210 t chiu rng hoc chiu cao: </p>
    211 
    212 <ul>
    213   <li><var>wrap_content</var> cho bit dng xem ca bn t nh c theo kch thc
    214 m ni dung ca n yu cu.</li>
    215   <li><var>match_parent</var> (c t tn <var>fill_parent</var> trc API Mc 8)
    216 cho bit dng xem ca bn c th phng ln khi nhm dng xem m ca n cho php.</li>
    217 </ul>
    218 
    219 <p>Nhn chung, vic ch nh mt chiu rng v chiu cao b tr bng cch s dng cc n v tuyt i nh
    220 im nh l iu khng c khuyn co. Thay vo , s dng cc s o tng i nh
    221 s n v im nh c lp vi mt  (<var>dp</var>), <var>wrap_content</var>, hoc
    222 <var>match_parent</var>, l mt phng php tt hn, v n gip m bo rng
    223 ng dng ca bn s hin th ph hp gia nhiu loi kch c mn hnh thit b khc nhau.
    224 Cc kiu s o c chp nhn c nh ngha trong ti liu
    225 <a href="{@docRoot}guide/topics/resources/available-resources.html#dimension">
    226 Ti nguyn C sn</a>.</p>
    227 
    228 
    229 <h2 id="Position">V tr B tr</h2>
    230    <p>
    231    Hnh hc ca mt dng xem l hnh ch nht. Dng xem c mt v tr,
    232    c biu din thnh mt cp ta  <em>tri</em> v <em>trn</em> v
    233    hai kch thc, c biu din thnh chiu rng v chiu cao. n v ca v tr
    234    v kch thc l im nh.
    235    </p>
    236 
    237    <p>
    238    C th truy xut v tr ca mt dng xem bng cch gi ra cc phng php
    239    {@link android.view.View#getLeft()} v {@link android.view.View#getTop()}. Phng php u tr v ta  tri, hay X,
    240    ca hnh ch nht biu din dng xem. Phng php sau tr v ta  trn, hay Y,
    241    ca hnh ch nht biu din dng xem. Nhng phng php ny
    242    u tr v v tr ca dng xem so vi dng xem m ca n. V d,
    243    khi <code>getLeft()</code> tr v 20, iu  c ngha l dng xem nm  20 im nh v
    244    bn phi ca cnh tri ca dng xem m trc tip ca n.
    245    </p>
    246 
    247    <p>
    248    Ngoi ra, mt vi phng php thun tin c a ra  trnh nhng tnh ton
    249    khng cn thit, c th l {@link android.view.View#getRight()} v {@link android.view.View#getBottom()}.
    250    Nhng phng php ny tr v ta  ca cnh phi v cnh y ca hnh ch nht
    251    biu din dng xem. V d, vic gi {@link android.view.View#getRight()}
    252    tng t nh tnh ton sau: <code>getLeft() + getWidth()</code>.
    253    </p>
    254 
    255 
    256 <h2 id="SizePaddingMargins">Kch c, Phn m v L</h2>
    257    <p>
    258    Kch c ca mt dng xem c biu din bng chiu rng v chiu cao. Thc ra mt dng xem
    259    s c hai cp gi tr chiu rng v chiu cao.
    260    </p>
    261 
    262    <p>
    263    Cp th nht c gi l <em>chiu rng o c</em> v
    264    <em>chiu cao o c</em>. Nhng kch thc ny xc nh mt dng xem mun phng ln bao nhiu
    265    trong dng xem m ca n. Cc
    266    kch thc o c c th thu c bng cch gi {@link android.view.View#getMeasuredWidth()}
    267    v {@link android.view.View#getMeasuredHeight()}.
    268    </p>
    269 
    270    <p>
    271    Cp th hai n thun l <em>chiu rng</em> v <em>chiu cao</em>, hoc
    272    i khi gi l <em>chiu rng v</em> v <em>chiu cao v</em>. Nhng
    273    kch thc ny xc nh kch c thc s ca dng xem trn mn hnh, ti thi im v v
    274    sau khi b tr. Nhng gi tr ny c th nhng khng nht thit phi khc vi
    275    chiu rng v chiu cao o c. Chiu rng v chiu cao ny c th ly c bng cch gi
    276    {@link android.view.View#getWidth()} v {@link android.view.View#getHeight()}.
    277    </p>
    278 
    279    <p>
    280     o cc kch thc ca n, dng xem cn xt ti phn m ca n. Phn m
    281    c biu din bng s im nh ca phn bn tri, bn trn, bn phi v bn di ca dng xem.
    282    Phn m c th c s dng  b tr ni dung ca dng xem bng mt s im nh
    283    c th. V d, phn m bn tri bng 2 s y ni dung ca dng xem i
    284    2 im nh v bn phi ca cnh bn tri. Phn m c th c t bng cch s dng phng php
    285    {@link android.view.View#setPadding(int, int, int, int)} v c truy vn bng cch gi
    286    {@link android.view.View#getPaddingLeft()}, {@link android.view.View#getPaddingTop()},
    287    {@link android.view.View#getPaddingRight()} v {@link android.view.View#getPaddingBottom()}.
    288    </p>
    289 
    290    <p>
    291    Mc d dng xem c th xc nh phn m, n khng c bt k s h tr no cho
    292    l. Tuy nhin, cc nhm dng xem li cung cp s h tr nh vy. Tham kho
    293    {@link android.view.ViewGroup} v
    294    {@link android.view.ViewGroup.MarginLayoutParams}  bit thm thng tin.
    295    </p>
    296 
    297    <p> bit thm thng tin v kch thc, xem
    298    <a href="{@docRoot}guide/topics/resources/more-resources.html#Dimension">Gi tr Kch thc</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">Cc B tr Thng gp</h2>
    322 
    323 <p>Mi lp con ca lp {@link android.view.ViewGroup} cung cp mt cch duy nht  hin th
    324 cc dng xem m bn lng trong n. Di y l mt s kiu b tr ph bin hn m c tch hp
    325 trong nn tng Android.</p>
    326 
    327 <p class="note"><strong>Lu :</strong> Mc d bn c th lng mt hoc nhiu b tr vi mt
    328 b tr khc  t c thit k UI ca mnh, bn nn c gng duy tr phn cp b tr ca mnh  mc nng nht
    329 c th. B tr ca bn s v nhanh hn nu n c t b tr lng nhau hn (phn cp dng xem rng
    330 s tt hn phn cp dng xem su).</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">B tr Tuyn tnh</a></h4>
    347   <a href="layout/linear.html"><img src="{@docRoot}images/ui/linearlayout-small.png" alt="" /></a>
    348   <p>Mt b tr c chc nng sp xp t chc cc b tr con ca n thnh mt hng ngang hoc thng ng. N
    349   s to mt thanh cun nu chiu di ca ca s vt qu chiu di ca mn hnh.</p>
    350 </div>
    351 
    352 <div class="layout">
    353   <h4><a href="layout/relative.html">B tr Tng i</a></h4>
    354   <a href="layout/relative.html"><img src="{@docRoot}images/ui/relativelayout-small.png" alt="" /></a>
    355   <p>Cho php bn ch nh v tr ca cc i tng con so vi nhau (i tng con A v pha
    356 bn tri ca i tng con B) hoc so vi i tng m (c cn theo bn trn i tng m).</p>
    357 </div>
    358 
    359 <div class="layout">
    360   <h4><a href="{@docRoot}guide/webapps/webview.html">Dng xem Web</a></h4>
    361   <a href="{@docRoot}guide/webapps/webview.html"><img src="{@docRoot}images/ui/webview-small.png" alt="" /></a>
    362   <p>Hin th trang web.</p>
    363 </div>
    364 
    365 
    366 
    367 
    368 <h2 id="AdapterViews" style="clear:left">Xy dng B tr bng mt Trnh iu hp</h2>
    369 
    370 <p>Khi ni dung cho b tr ca bn ng hoc cha c xc nh trc, bn c th s dng mt b tr c chc nng
    371 to lp con {@link android.widget.AdapterView}  a vo b tr c dng xem vo thi gian chy. Mt
    372 lp con ca lp {@link android.widget.AdapterView} s s dng mt {@link android.widget.Adapter} 
    373 gn kt d liu vi b tr ca n. {@link android.widget.Adapter} ng vai tr trung gian gia ngun
    374 d liu v b tr {@link android.widget.AdapterView} &mdash;{@link android.widget.Adapter}
    375 s truy xut d liu (t mt ngun chng hn nh mt mng hoc truy vn c s d liu) v chuyn tng mc nhp
    376 thnh mt dng xem c th thm vo b tr {@link android.widget.AdapterView}.</p>
    377 
    378 <p>Cc b tr ph bin c h tr bi trnh iu hp bao gm:</p>
    379 
    380 <div class="layout first">
    381   <h4><a href="layout/listview.html">Dng xem Danh sch</a></h4>
    382   <a href="layout/listview.html"><img src="{@docRoot}images/ui/listview-small.png" alt="" /></a>
    383   <p>Hin th mt danh sch ct cun n.</p>
    384 </div>
    385 
    386 <div class="layout">
    387   <h4><a href="layout/gridview.html">Dng xem Li</a></h4>
    388   <a href="layout/gridview.html"><img src="{@docRoot}images/ui/gridview-small.png" alt="" /></a>
    389   <p>Hin th mt li cun gm nhiu hng v ct.</p>
    390 </div>
    391 
    392 
    393 
    394 <h3 id="FillingTheLayout" style="clear:left">in d liu vo mt dng xem trnh iu hp</h3>
    395 
    396 <p>Bn c th a vo mt {@link android.widget.AdapterView} chng hn nh {@link android.widget.ListView} hoc
    397 {@link android.widget.GridView} bng cch gn kt thc th {@link android.widget.AdapterView} vi mt
    398 {@link android.widget.Adapter}, n truy xut d liu t mt ngun bn ngoi v to mt {@link
    399 android.view.View}  biu din tng mc nhp d liu.</p>
    400 
    401 <p>Android cung cp mt vi lp con ca {@link android.widget.Adapter} rt hu ch cho vic
    402 truy xut cc kiu d liu khc nhau v xy dng dng xem cho mt {@link android.widget.AdapterView}. Hai
    403 trnh iu hp ph bin nht l:</p>
    404 
    405 <dl>
    406   <dt>{@link android.widget.ArrayAdapter}</dt>
    407     <dd>S dng trnh iu hp ny khi ngun d liu ca bn l mt mng. Theo mc nh, {@link
    408 android.widget.ArrayAdapter} to mt dng xem cho mi mc mng bng cch gi {@link
    409 java.lang.Object#toString()} trn tng mc v t ni dung trong mt {@link
    410 android.widget.TextView}.
    411       <p>V d, nu bn c mt mng xu m bn mun hin th trong mt {@link
    412 android.widget.ListView}, hy khi to mt {@link android.widget.ArrayAdapter} mi bng cch s dng
    413 hm dng  ch nh b tr cho tng xu v mng xu:</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>Cc tham i cho hm dng ny l:</p>
    419 <ul>
    420   <li>ng dng ca bn {@link android.content.Context}</li>
    421   <li>B tr cha mt {@link android.widget.TextView} cho mi xu trong mng</li>
    422   <li>Mng xu</li>
    423 </ul>
    424 <p>Sau  ch cn gi
    425 {@link android.widget.ListView#setAdapter setAdapter()} trn {@link android.widget.ListView} ca bn:</p>
    426 <pre>
    427 ListView listView = (ListView) findViewById(R.id.listview);
    428 listView.setAdapter(adapter);
    429 </pre>
    430 
    431       <p> ty chnh din mo ca tng mc, bn c th khng ch phng php {@link
    432 java.lang.Object#toString()} cho cc i tng trong mng ca mnh. Hoc,  to mt dng xem cho tng
    433 mc khng phi l mt {@link android.widget.TextView} (v d, nu bn mun mt
    434 {@link android.widget.ImageView} cho tng mc mng), hy m rng lp {@link
    435 android.widget.ArrayAdapter} v khng ch {@link android.widget.ArrayAdapter#getView
    436 getView()}  tr v kiu dng xem m bn mun cho tng mc.</p>
    437 
    438 </dd>
    439 
    440   <dt>{@link android.widget.SimpleCursorAdapter}</dt>
    441     <dd>S dng trnh iu hp ny khi d liu ca bn n t mt {@link android.database.Cursor}. Khi
    442 s dng {@link android.widget.SimpleCursorAdapter}, bn phi ch nh mt b tr  s dng cho tng
    443 hng trong {@link android.database.Cursor} v nhng ct no trong {@link android.database.Cursor}
    444 nn c chn vo dng xem no ca b tr. V d, nu bn mun to mt danh sch
    445 tn ngi v s in thoi, bn c th thc hin mt truy vn m tr v mt {@link
    446 android.database.Cursor} cha mt hng cho tng ngi v nhiu ct cho cc tn v
    447 s in thoi. Sau , bn to mt mng xu ch nh nhng ct no t {@link
    448 android.database.Cursor} m bn mun trong b tr cho tng kt qu v mt mng s nguyn ch nh cc
    449 dng xem tng ng m tng ct s c t vo:</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>Khi bn khi to {@link android.widget.SimpleCursorAdapter}, hy chuyn b tr cn s dng cho
    456 tng kt qu, {@link android.database.Cursor} cha cc kt qu, v hai mng sau:</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>Sau , {@link android.widget.SimpleCursorAdapter} to mt dng xem cho tng hng trong
    464 {@link android.database.Cursor} s dng b tr c cung cp bng cch chn tng mc {@code
    465 fromColumns} vo dng xem {@code toViews} tng ng.</p>.</dd>
    466 </dl>
    467 
    468 
    469 <p>Trong vng i ng dng ca bn, nu bn thay i d liu lin quan c c bi
    470 trnh iu hp ca mnh, bn nn gi {@link android.widget.ArrayAdapter#notifyDataSetChanged()}. N s
    471 thng bo vi dng xem nh km rng d liu  c thay i v dng xem nn t lm mi.</p>
    472 
    473 
    474 
    475 <h3 id="HandlingUserSelections">X l s kin nhp</h3>
    476 
    477 <p>Bn c th phn hi cc s kin nhp trn tng mc trong mt {@link android.widget.AdapterView} bng cch
    478 trin khai giao din {@link android.widget.AdapterView.OnItemClickListener}. V d:</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