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">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">Activity</a> <a href="{@docRoot}guide/topics/appwidgets/index.html"></a>) 42 </p> 43 <ul> 44 <li><strong> XML UI </strong>Android XML View () 45 </li> 46 <li><strong></strong> View ViewGroup () 47 </li> 48 </ul> 49 50 <p>Android UI 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">Eclipse ADT </a> XML — XML []<strong></strong> 56 57 </li> 58 <li> <a href="{@docRoot}tools/debugging/debugging-ui.html#hierarchyViewer"></a> — / () 59 60 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 UI UI XML XML UI XML View {@link android.view.ViewGroup} {@link android.view.View} 69 70 </p> 71 72 <p> UI XML XML XML EditText <code>EditText.setText()</code> <code>text</code> 73 74 </p> 75 76 <p class="note"><strong></strong><a href="{@docRoot}guide/topics/ui/layout-objects.html"></a> 77 <a href="{@docRoot}resources/tutorials/views/index.html">Hello Views</a> 78 </p> 79 80 <h2 id="write"> XML</h2> 81 82 <p> Android XML UI HTML — </p> 83 84 <p> 1 ( View ViewGroup ) {@link android.widget.LinearLayout} 85 {@link android.widget.TextView} {@link android.widget.Button} XML </p> 86 <pre> 87 <?xml version="1.0" encoding="utf-8"?> 88 <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 <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 <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 </LinearLayout> 101 </pre> 102 103 <p> XML <code>.xml</code> Android <code>res/layout/</code> 104 </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} {@link android.app.Activity#onCreate(android.os.Bundle) Activity.onCreate()} <code>{@link android.app.Activity#setContentView(int) setContentView()}</code> 112 <code>R.layout.<em>layout_file_name</em></code> XML <code>main_layout.xml</code> Activity XML 113 114 115 116 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>Android Activity Activity <code>onCreate()</code> ( <a href="{@docRoot}guide/components/activities.html#Lifecycle">Activity</a>) 126 127 128 </p> 129 130 131 <h2 id="attributes"></h2> 132 133 <p> View ViewGroup XML View ( TextView <code>textSize</code> ) View View View ( <code>id</code> ) 134 135 136 137 View (View ViewGroup ) 138 139 </p> 140 141 <h3 id="id">ID</h3> 142 143 <p> View ID View ID XML <code>id</code> View XML ( {@link android.view.View} ) XML ID 144 145 146 147 148 </p> 149 <pre>android:id="@+id/my_button"</pre> 150 151 <p> @ XML ID ID 152 (+) (<code>R.java</code> ) 153 Android ID 154 Android ID + <code>android</code> 155 </p> 156 <pre>android:id="@android:id/empty"</pre> 157 <p> <code>android</code> <code>android.R</code> ID 158 </p> 159 160 <p></p> 161 <ol> 162 <li>/ ID 163 <pre> 164 <Button android:id="@+id/my_button" 165 android:layout_width="wrap_content" 166 android:layout_height="wrap_content" 167 android:text="@string/my_button_text"/> 168 </pre> 169 </li> 170 <li> ( <code>{@link android.app.Activity#onCreate(Bundle) onCreate()}</code> ) 171 172 <pre> 173 Button myButton = (Button) findViewById(R.id.my_button); 174 </pre> 175 </li> 176 </ol> 177 <p> {@link android.widget.RelativeLayout} ID ID 178 179 </p> 180 <p> ID ID ID ID ( ID) 181 182 </p> 183 184 185 <h3 id="layout-params"></h3> 186 187 <p> <code>layout_<em>something</em></code> XML ViewGroup View 188 </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 LayoutParams 202 </p> 203 204 <p> (<code>layout_width</code> 205 <code>layout_height</code>) LayoutParams 206 <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><a href="{@docRoot}guide/topics/resources/available-resources.html#dimension"></a> 223 224 225 226 </p> 227 228 229 <h2 id="Position"></h2> 230 <p> 231 (<em></em><em></em>) () 232 233 234 235 </p> 236 237 <p> 238 {@link android.view.View#getLeft()} {@link android.view.View#getTop()} 239 (X ) 240 (Y ) 241 242 <code>getLeft()</code> 20 20 243 244 245 </p> 246 247 <p> 248 {@link android.view.View#getRight()} {@link android.view.View#getBottom()} 249 250 251 {@link android.view.View#getRight()} <code>getLeft() + getWidth()</code> 252 253 </p> 254 255 256 <h2 id="SizePaddingMargins"></h2> 257 <p> 258 259 260 </p> 261 262 <p> 263 <em></em><em></em> 264 265 {@link android.view.View#getMeasuredWidth()} {@link android.view.View#getMeasuredHeight()} 266 267 268 </p> 269 270 <p> 271 <em></em><em></em><em></em><em></em> 272 () 273 274 () 275 276 {@link android.view.View#getWidth()} {@link android.view.View#getHeight()} 277 </p> 278 279 <p> 280 () 281 282 283 2 2 284 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><a href="{@docRoot}guide/topics/resources/more-resources.html#Dimension"></a> 298 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 Android 325 </p> 326 327 <p class="note"><strong></strong> UI 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 — 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> ( A B ) () 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> {@link android.widget.AdapterView} 371 372 {@link android.widget.AdapterView} {@link android.widget.Adapter} 373 {@link android.widget.Adapter} {@link android.widget.AdapterView} — {@link android.widget.Adapter} {@link android.widget.AdapterView} 374 375 376 </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} 397 {@link android.widget.Adapter} {@link 398 android.view.View} {@link android.widget.AdapterView} ( {@link android.widget.ListView} {@link android.widget.GridView} 399 </p> 400 401 <p>Android {@link android.widget.Adapter} {@link android.widget.AdapterView} 402 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<String> adapter = new ArrayAdapter<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> {@link android.widget.ListView} 425 {@link android.widget.ListView#setAdapter setAdapter()}</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()} {@link android.widget.TextView} ( {@link android.widget.ImageView}) {@link 433 android.widget.ArrayAdapter} {@link android.widget.ArrayAdapter#getView 434 getView()} 435 436 </p> 437 438 </dd> 439 440 <dt>{@link android.widget.SimpleCursorAdapter}</dt> 441 <dd> {@link android.database.Cursor} 442 {@link android.widget.SimpleCursorAdapter} {@link android.database.Cursor} {@link android.database.Cursor} 443 444 {@link 445 android.database.Cursor} 446 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} {@link android.database.Cursor} 456 </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} {@code 464 fromColumns} {@code toViews} 465 {@link android.database.Cursor} </p>.</dd> 466 </dl> 467 468 469 <p> {@link android.widget.ArrayAdapter#notifyDataSetChanged()} 470 471 </p> 472 473 474 475 <h3 id="HandlingUserSelections"></h3> 476 477 <p> {@link android.widget.AdapterView.OnItemClickListener} {@link android.widget.AdapterView} 478 </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