LECTURE 3 ¥ê¥½¡¼¥¹¤Ë¤Ä¤¤¤Æ

¡û ¤Ï¤¸¤á¤Ë

Android SDK¡Ê¤È¤¤¤¦¤è¤ê Android ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¡Ë¤Ç¤Ï¡¢Ê¸»úÎó¤ä²èÁüÅù¤ò¥ê¥½¡¼¥¹¤È¤·¤Æ°·¤¦¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£¥ê¥½¡¼¥¹²½¤·¤¿¥Ç¡¼¥¿¤ÏÁ´¤Æ¡¢SDK ¤Ë¤è¤Ã¤Æ¼«Æ°À¸À®¤µ¤ì¤ë R.java ¤ËÄêµÁ¤µ¤ì¤ë¥ê¥½¡¼¥¹ ID ¤òÍѤ¤¤Æ´Êñ¤Ë»²¾È¤¹¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£

¤³¤³¤Ç¤Ï¡¢Android SDK ¤Ç¤É¤ÎÍͤʥǡ¼¥¿¤ò¥ê¥½¡¼¥¹¤È¤·¤Æ°·¤¦¤³¤È¤¬¤Ç¤­¤ë¤Î¤«¡¢³µÍפòÄ´¤Ù¤Æ¤ß¤¿¤¤¤È»×¤¤¤Þ¤¹¡£¼«Ê¬¤âÁ´¤Æ»È¤Ã¤Æ¤ß¤¿¤ï¤±¤Ç¤Ï̵¤¤¤Î¤Ç¡¢¤è¤¯Ê¬¤«¤Ã¤Æ¤¤¤Ê¤¤Éôʬ¤â¤¢¤ê¤Þ¤¹¡£¤è¤ê¶ñÂÎŪ¤Ë¤Ï¡¢¼ÂºÝ¤Ë¤½¤ì¤é¤Î¥ê¥½¡¼¥¹¤ò»ÈÍѤ¹¤ëºÝ¤Ë¿ï»þÀâÌÀ¤·¤¿¤¤¤È»×¤¤¤Þ¤¹¡£

¡û ¥ê¥½¡¼¥¹¤Î¼ïÎà¤È³ÊǼ¾ì½ê

Android SDK ¤Ç°·¤¨¤ë¥ê¥½¡¼¥¹¤Î¼ïÎà¤Ë¤Ä¤¤¤Æ¤Ï¡¢App Resources¤ÇÀâÌÀ¤µ¤ì¤Æ¤¤¤Þ¤¹¡£Resource Types¤ÇÀâÌÀ¤µ¤ì¤Æ¤¤¤ëÆâÍƤò°ÕÌõ¤·¤Þ¤¹¡£¤Ê¤ª¡¢´ðËÜŪ¤Ë¤Ï¥Õ¥¡¥¤¥ë̾¤Ç¤Ï¤Ê¤¯¡¢ÍÑ°Õ¤·¤¿ xml ¥Õ¥¡¥¤¥ë¤Î¥¿¥°¤Ç¥ê¥½¡¼¥¹¤¬È½Ê̤µ¤ì¤ëÍͤǤ¹¡£

¥ê¥½¡¼¥¹¤Î¼ïÎà res/°Ê²¼¤Î³ÊǼ¾ì½ê ÆâÍÆ
¥¢¥Ë¥á¡¼¥·¥ç¥ó res/anim¡¢res/drawable ͽ¤áÆ°¤­¤¬·èÄꤵ¤ì¤¿¥¢¥Ë¥á¡¼¥·¥ç¥ó¤òÄêµÁ¤·¤Þ¤¹¡£¤¢¤ë²èÁü¤ËÂФ¹¤ëÊÑ·ÁÅù¤Î¥¨¥Õ¥§¥¯¥È¤Ë¤è¤ë¥¢¥Ë¥á¡¼¥·¥ç¥ó¤Î¾ì¹ç¤Ïres/anim¤Ë¡¢Ê£¿ô²èÁü¤ò·Ò¤°¤³¤È¤Ë¤è¤ë¥¢¥Ë¥á¡¼¥·¥ç¥ó¤Î¾ì¹ç¤Ïres/drawable¤Ëxml¥Õ¥¡¥¤¥ë¤òÃÖ¤­¤Þ¤¹¡£
View¾õÂ֤˱þ¤¸¤¿¿§ res/color android.view.View¥¯¥é¥¹¡Ê¤ª¤è¤ÓÇÉÀ¸¤·¤¿¥¦¥£¥¸¥§¥Ã¥È¡Ë¤¬»ý¤Ä¾õÂ֤˱þ¤¸¤ÆÊѲ½¤µ¤»¤ë¿§¤òÄêµÁ¤·¤Þ¤¹¡£res/color¤Ëxml¥Õ¥¡¥¤¥ë¤òÃÖ¤­¤Þ¤¹¡£
²èÌ̤Ëɽ¼¨¤¹¤ë¥°¥é¥Õ¥£¥Ã¥¯ÄêµÁ¡Ê²èÁü¡¢¾õÂ֤˱þ¤¸¤ÆÊѲ½¤¹¤ë¥°¥é¥Õ¥£¥Ã¥¯¡¢Â¾¤Îɽ¼¨¤ò¥¯¥ê¥Ã¥×¤¹¤ë¥°¥é¥Õ¥£¥Ã¥¯Åù¡Ë res/drawable ²èÌ̤Ëɽ¼¨¤¹¤ë¥°¥é¥Õ¥£¥Ã¥¯¤òÄêµÁ¤·¤¿¥Õ¥¡¥¤¥ë¤òres/drawable¤ËÃÖ¤­¤Þ¤¹¡£²èÁü¤Î¾ì¹ç¤Ï *.png¡¢*.jpg¡¢*.gif¡¢*.9.png¤ò¥µ¥Ý¡¼¥È¤·¤Æ¤¤¤Þ¤¹¡£
UI ¥ì¥¤¥¢¥¦¥È res/layout UI ¾å¤Ëɽ¼¨¤¹¤ë¥¦¥£¥¸¥§¥Ã¥ÈÅù¤Î¥ì¥¤¥¢¥¦¥È¤òµ­½Ò¤·¤¿ xml ¥Õ¥¡¥¤¥ë¤òÃÖ¤­¤Þ¤¹¡£Android ¤Ç¤Ï¡¢¥×¥í¥°¥é¥à¥³¡¼¥É¤Ç UI ¤òºîÀ®¤¹¤ë¤³¤È¤â¤â¤Á¤í¤ó²Äǽ¤Ç¤¹¤¬¡¢¤É¤Á¤é¤«¤È¤¤¤¦¤È xml ¥Õ¥¡¥¤¥ë¤Ë¥ì¥¤¥¢¥¦¥È¤ò½ñ¤¯¤³¤È¤Ç¥³¡¼¥É¤È UI ¥ì¥¤¥¢¥¦¥È¤òʬΥ¤¹¤ëÊýË¡¤¬¤ª´«¤á¤ÎÍͤǤ¹¡£Á°²ó ¤Î MainActivity.java Ãæ¤Ç setContentView(R.layout.main); ¤È¤·¤Æ¤¤¤ëÉôʬ¤¬¡¢xml ¤Ëµ­½Ò¤·¤¿¥ì¥¤¥¢¥¦¥È¤òŬÍѤ·¤Æ¤¤¤ë²Õ½ê¤Ç¤¹¡£
¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¥á¥Ë¥å¡¼ res/menu ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¥á¥Ë¥å¡¼¡Ê¥ª¥×¥·¥ç¥ó¥á¥Ë¥å¡¼¡¢¥³¥ó¥Æ¥­¥¹¥È¥á¥Ë¥å¡¼¡¢¥µ¥Ö¥á¥Ë¥å¡¼¡Ë¤òÄêµÁ¤·¤¿ xml ¥Õ¥¡¥¤¥ë¤òÃÖ¤­¤Þ¤¹¡£
ʸ»úÎó res/values ʸ»úÎó¤ä¡¢Ê¸»úÎó¤ÎÇÛÎóÅù¤òÄêµÁ¤·¤¿ xml ¥Õ¥¡¥¤¥ë¤òÃÖ¤­¤Þ¤¹¡£
UI ¥¹¥¿¥¤¥ë res/values UI ¥¹¥¿¥¤¥ë¡Ê¥Õ¥©¥ó¥È¥µ¥¤¥º¡¢¥Õ¥©¥ó¥È¿§Åù¡Ë¤òÄêµÁ¤·¤¿ xml ¥Õ¥¡¥¤¥ë¤òÃÖ¤­¤Þ¤¹¡£
¤½¤Î¾ res/values ¤¤¤í¤ó¤Ê¥ê¥½¡¼¥¹¤òÄêµÁ¤·¤¿ xml ¥Õ¥¡¥¤¥ë¤ò¤³¤³¤ËÃÖ¤¯¤³¤È¤¬½ÐÍè¤Þ¤¹¡£²¼µ­¤Î¥ê¥½¡¼¥¹¤¬ÄêµÁ²Äǽ¤Ç¤¹¡£
  • Bool ¡ÊÏÀÍýÃÍ¡Ë
  • Color ¡Ê¿§ÄêµÁ:colors.xml¡Ë
  • Dimension ¡Êmm¡¢px Åù¡¢Ã±°Ì¤ò»ý¤Ã¤¿¿ôÃÍ¡Ë
  • ID (¥æ¥Ë¡¼¥¯¤ÊIDÃÍ¡£Android developer tools ¤Ç¥æ¥Ë¡¼¥¯ÃͤòºîÀ®²Äǽ )
  • Integer (À°¿ô)
  • Integer Array (À°¿ôÇÛÎó)
  • Typed Array (¾¤Î¥ê¥½¡¼¥¹¤ÎÇÛÎó)
¤è¤ê¾ÜºÙ¤Ë¤Ï¡¢SDK ¥É¥­¥å¥á¥ó¥È¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£ËÜʸ½ñ¤Ç¤â¡¢¼ÂºÝ¤Ë»È¤¦»þ¤Ë¤ÏÀâÌÀ¤·¤Þ¤¹¡£

¡û UI ¥ì¥¤¥¢¥¦¥ÈÄêµÁ¥Õ¥¡¥¤¥ë

UI ¥ì¥¤¥¢¥¦¥ÈÄêµÁ¥Õ¥¡¥¤¥ë¤Ï¡¢res/layout °Ê²¼¤Ë xml ¥Õ¥¡¥¤¥ë¤È¤·¤Æ³ÊǼ¤·¤Þ¤¹¡£²èÌ̤Ëɽ¼¨¤¹¤ë android.view.View¡Ê¥µ¥Ö¥¯¥é¥¹¡Ë¤Î¥Ä¥ê¡¼¹½Â¤¤òµ­½Ò¤·¤Þ¤¹¡£

Îã¤È¤·¤Æ Á°²ó ¤Î UI ¥ì¥¤¥¢¥¦¥ÈÄêµÁ¥Õ¥¡¥¤¥ë¤Ç¤¢¤ë res/layout/main.xml ¤ò°Ê²¼¤Ë·ÇºÜ¤·¤Þ¤¹¡£

    1	<?xml version="1.0" encoding="utf-8"?>
    2	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3	    android:orientation="vertical"
    4	    android:layout_width="fill_parent"
    5	    android:layout_height="fill_parent"
    6	    >
    7	<TextView
    8	    android:layout_width="fill_parent"
    9	    android:layout_height="wrap_content"
   10	    android:text="Hello World, MainActivity"
   11	    />
   12	</LinearLayout>
   13	

¤³¤Î¥Õ¥¡¥¤¥ë¤ÇÄêµÁ¤µ¤ì¤Æ¤¤¤ë¥Ä¥ê¡¼¹½Â¤¤Ï

  • android.widget.LinearLayout
    • android.widget.TextView

¤Ç¤¹¡£android.widget.LinearLayout ¤Ï¡¢Ê£¿ô¤Î View ¤ò²èÌ̾å¤Ë¥ì¥¤¥¢¥¦¥È¤¹¤ë¤¿¤á¤Î¥ì¥¤¥¢¥¦¥È´ÉÍýÍÑ¥¯¥é¥¹¤Î°ì¤Ä¤Ç¤¹¡Êandroid.view.View ¤Î¥µ¥Ö¥¯¥é¥¹¤Ç¤¢¤ë android.view.ViewGroup ¤ò·Ñ¾µ¤·¤Æ¤¤¤ë¡Ë¡£android.widget.TextView ¤Ï¡¢Ê¸»úÎóɽ¼¨ÍѤΠUI ÉôÉʤǤ¹¡£

¥Ý¥¤¥ó¥È¤Ï¡¢°Ê²¼¤ÎÍͤʴ¶¤¸¤Ç¤¹¡£

  • ºÇ¾å°Ì¤Î View ¤Î¥¿¥°¤Ë¤Ï xmlns:android="http://schemas.android.com/apk/res/android" ¤òÉÕ¤±¤ë
  • ³Æ View ¤Î¥¿¥°¤Ë android:xxxx ·Á¼°¤Î°À­¤ò»ØÄꤹ¤ë¤³¤È¤Ç¡¢¥×¥í¥Ñ¥Æ¥£¤òÀßÄê¤Ç¤­¤ë
  • android:layout_width/android:layout_height ¤Ë»ØÄꤷ¤Æ¤¤¤ë fill_parent/wrap_content ¤Ï¤½¤ì¤¾¤ì "¿ÆÍ×ÁǤ¤¤Ã¤Ñ¤¤¤Ë¹­¤²¤ë/ɽ¼¨ÆâÍƤ˱þ¤¸¤¿Â礭¤µ¤Ë¤¹¤ë" ¤È¤¤¤¦°ÕÌ£

¡û ʸ»úÎó¥ê¥½¡¼¥¹ÄêµÁ¥Õ¥¡¥¤¥ë

ʸ»úÎó¥ê¥½¡¼¥¹ÄêµÁ¥Õ¥¡¥¤¥ë¤Ï res/values/strings.xml ¤È¤·¤Æ³ÊǼ¤·¤Þ¤¹¡£Îã¤È¤·¤Æ Á°²ó ¤Îʸ»úÎó¥ê¥½¡¼¥¹ÄêµÁ¥Õ¥¡¥¤¥ë¤Ç¤¢¤ë res/values/strings.xml ¤ò°Ê²¼¤Ë·ÇºÜ¤·¤Þ¤¹¡£

    1	<?xml version="1.0" encoding="utf-8"?>
    2	<resources>
    3	    <string name="app_name">MainActivity</string>
    4	</resources>

<resources></resources> ¤Ç°Ï¤ó¤ÀÈϰϤˡ¢<string name="¥ê¥½¡¼¥¹Ì¾">ʸ»úÎó</string> ¤È¤·¤Æʸ»úÎó¥ê¥½¡¼¥¹¤òÄêµÁ¤·¤Þ¤¹¡£

¡û ¿§¥ê¥½¡¼¥¹ÄêµÁ¥Õ¥¡¥¤¥ë

¿§¥ê¥½¡¼¥¹ÄêµÁ¥Õ¥¡¥¤¥ë¤Ï res/values/colors.xml ¤È¤·¤Æ³ÊǼ¤·¤Þ¤¹¡£°Ê²¼¤ËÎã¤ò·ÇºÜ¤·¤Þ¤¹¡£

    1	<?xml version="1.0" encoding="utf-8"?>
    2	<resources>
    3	    <color name="red">#ff0000</color>
    4	    <color name="green">#00ff00</color>
    5	    <color name="blue">#0000ff</color>
    6	</resources>

<resources></resources> ¤Ç°Ï¤ó¤ÀÈϰϤˡ¢<color name="¥ê¥½¡¼¥¹Ì¾">¿§ÄêµÁ</color> ¤È¤·¤Æ¿§¥ê¥½¡¼¥¹¤òÄêµÁ¤·¤Þ¤¹¡£¿§ÄêµÁ¤Ï¡¢°Ê²¼¤Î¤¤¤º¤ì¤«¤Î·Á¼°¤Çµ­½Ò¤·¤Þ¤¹¡£

  • #RGB
  • #ARGB
  • #RRGGBB
  • #AARRGGBB

RGB ¤Ï¤½¤ì¤¾¤ìÀÖÎÐÀÄÀ®Ê¬¤Ç¤¹¡£A ¤Ï Alpha À®Ê¬¤òɽ¤·¤Æ¤¤¤Æ¡¢0(AA ¤Î¾ì¹ç¤Ï 00)¤Ç´°Á´¤ËÆ©ÌÀ¡¢f(AA ¤Î¾ì¹ç¤Ï ff)¤ÇÈóÆ©ÌÀ¤òɽ¤·¤Þ¤¹¡£