µ¼¶Á ¾àÀë°²µÏ?³±öºÍËûµÄÍŶÓ×ÅÊÖ¿ª·¢Ò»¸öÏ£Íûµß¸²´«Í³ÊÖ»ú²Ù×÷ģʽµÄ²Ù×÷ϵͳÒѾ¹ýÈ¥ 12 ÄêÁË£¬ÕâÌ×ϵͳÓпÉÄÜÈÃÊÖ»ú»òÕßÖÇÄÜ»ú¸øÏû·ÑÕßÒÔ¼°Èí¼þ¿ª·¢ÈËÔ±´øÀ´È«ÐµÄÌåÑ顣֮ǰµÄÖÇÄÜ»ú½öÏÞÓÚÊÕ·¢¶ÌÐźͲ鿴µç×ÓÓʼþ£¨µ±È»»¹¿ÉÒÔ´òµç»°£©£¬¸øÓû§ºÍ¿ª·¢Õß´øÀ´ºÜ´óµÄÏÞÖÆ¡£
°²×¿£¬×÷Ϊ´òÆÆÕâ¸ö¼ÏËøµÄϵͳ£¬ÓµÓзdz£ÓÅÐãµÄ¿ò¼ÜÉè¼Æ£¬¸ø´ó¼ÒÌṩµÄ²»½ö½öÊÇÒ»×éÓÐÏ޵ŦÄÜ£¬¸ü¶àµÄÊÇ×ÔÓɵÄ̽Ë÷¡£ÓÐÈË»á˵ iPhone ²ÅÊÇÊÖ»ú²úÒµµÄµß¸²²úÆ·£¬²»¹ýÎÒÃÇ˵µÄ²»ÊÇ iPhone Óжàô¿á£¨»òÕ߶àô¹ó£¬ÊÇ°É£¿£©£¬Ëü»¹ÊÇÓÐÏÞÖƵģ¬¶øÕâÊÇÎÒÃÇ´ÓÀ´¶¼²»Ï£ÍûÓеġ£
²»¹ý£¬¾ÍÏñ±¾´óÊå˵µÄ£¬ÄÜÁ¦Ô½´óÔðÈÎÔ½´ó£¬ÎÒÃÇÒ²ÐèÒª¸ü¼ÓÈÏÕæ¶Ô´ý°²×¿Ó¦ÓõÄÉè¼Æ·½Ê½¡£ÎÒ¿´µ½ºÜ¶à½Ì³Ì¶¼ºöÂÔÁËÏò³õѧÕß´«µÝÕâ¸öÀíÄÔÚ¶¯ÊÖ֮ǰÇëÏȳä·ÖÀí½âϵͳ¼Ü¹¹¡£ËûÃÇÖ»ÊÇ°ÑÒ»¶ÑµÄ¸ÅÄîºÍ´úÂ붪¸ø¶ÁÕߣ¬È´Ã»ÓнâÊÍÇå³þÏà¹ØµÄÓÅȱµã£¬ËüÃǶÔϵͳµÄÓ°Ï죬ÒÔ¼°¸ÃÓÃʲô²»¸ÃÓÃʲôµÈµÈ¡£ÔÚÕâƪÎÄÕÂÀÎÒÃǽ«½éÉÜһЩ³õѧÕßÒÔ¼°Öм¶¿ª·¢ÈËÔ±¶¼Ó¦¸ÃÕÆÎյļ¼ÇÉ£¬ÒÔ°ïÖú¸üºÃµØÀí½â°²×¿¿ò¼Ü¡£ºóÐøÎÒÃÇ»¹»áÔÚÕâ¸öϵÁÐÀïд¸ü¶àÕâÑùµÄ¹ØÓÚʵÓü¼ÇɵÄÎÄÕ¡£ÎÒÃÇ¿ªÊ¼°É¡£
1¡¢ @+id ºÍ @id µÄÇø±ð
ÒªÔÚ Java ´úÂëÀï·ÃÎÊÒ»¸öͼÐοؼþ£¨»ò×é¼þ£©£¬»òÕßÊÇÒªÈÃËü³ÉΪÆäËû¿Ø¼þµÄÒÀÀµ£¬ÎÒÃÇÐèÒªÒ»¸öΨһµÄÖµÀ´ÒýÓÃËü¡£Õâ¸öΨһֵÓà android:id ÊôÐÔÀ´¶¨Ò壬±¾ÖÊÉϾÍÊÇ°ÑÓû§ÌṩµÄ id ¸½¼Óµ½ @+id/ ºóÃ棬дÈëµ½ id ×ÊÔ´Îļþ£¬¹©ÆäËû¿Ø¼þʹÓá£Ò»¸ö Toolbar µÄ id ¿ÉÒÔÕâÑù¶¨Ò壬
android:id="@+id/toolbar"
È»ºóÕâ¸ö id Öµ¾ÍÄܱ» findViewById(¡) ʶ±ð£¬Õâ¸öº¯Êý»áÔÚ×ÊÔ´ÎļþÀï²éÕÒ id£¬»òÕßÖ±½Ó´Ó R.id ·¾¶ÒýÓã¬È»ºó·µ»ØËù²éÕÒµÄ View µÄÀàÐÍ¡£
¶øÁíÒ»ÖÖ£¬@id£¬ºÍ findViewById(¡) ÐÐΪһÑù - Ò²»á¸ù¾ÝÌṩµÄ id ²éÕÒ×é¼þ£¬²»¹ý½öÏÞÓÚ²¼¾ÖʱʹÓá£Ò»°ãÓÃÀ´²¼ÖÃÏà¹Ø¿Ø¼þ¡£
android:layout_below="@id/toolbar"
2¡¢ ʹÓà @string ×ÊԴΪ XML Ìṩ×Ö·û´®
¼òµ¥À´Ëµ£¬¾ÍÊDz»ÒªÔÚ XML ÀïÖ±½ÓÓÃ×Ö·û´®¡£ÔÒòºÜ¼òµ¥¡£µ±ÎÒÃÇÔÚ XML ÀïÖ±½ÓʹÓÃÁË×Ö·û´®£¬ÎÒÃÇÒ»°ã»áÔÚÆäËüµØ·½ÔÙ´ÎÓõ½Í¬ÑùµÄ×Ö·û´®¡£ÏëÏñһϵ±ÎÒÃÇÐèÒªÔÚ²»Í¬µÄµØ·½µ÷Õûͬһ¸ö×Ö·û´®µÄجÃΣ¬¶øÈç¹ûʹÓÃ×Ö·û´®×ÊÔ´¾ÍÖ»¸ÄÒ»¸öµØ·½¾Í¹»ÁË¡£ÁíÒ»¸öºÃ´¦ÊÇ£¬Ê¹ÓÃ×ÊÔ´Îļþ¿ÉÒÔÌṩ¶à¹úÓïÑÔÖ§³Ö£¬ÒòΪ¿ÉÒÔΪ²»Í¬µÄÓïÑÔ´´½¨ÏàÓ¦µÄ×Ö·û´®×ÊÔ´Îļþ¡£
android:text="My Awesome Application"
µ±ÄãÖ±½ÓʹÓÃ×Ö·û´®Ê±£¬Äã»áÔÚ Android Studio ÀïÊÕµ½¾¯¸æ£¬Ìáʾ˵Ӧ¸Ã°ÑдËÀµÄ×Ö·û´®¸Ä³É×Ö·û´®×ÊÔ´¡£¿ÉÒÔµã»÷Õâ¸öÌáʾ£¬È»ºó°´Ï ALT + ENTER ´ò¿ª×Ö·û´®±à¼¡£ÄãÒ²¿ÉÒÔÖ±½Ó´ò¿ª res Ŀ¼Ï嵀 values Ŀ¼ÀïµÄ strings.xml Îļþ£¬È»ºóÏñÏÂÃæÕâÑùÉùÃ÷Ò»¸ö×Ö·û´®×ÊÔ´¡£
My Awesome Application
È»ºóÓÃËüÀ´Ì滻дËÀµÄ×Ö·û´®£¬
android:text="@string/app_name"
3¡¢ ʹÓà @android ºÍ ?attr ³£Á¿
¾¡Á¿Ê¹ÓÃϵͳԤÏȶ¨ÒåµÄ³£Á¿¶ø²»ÊÇÖØÐÂÉùÃ÷¡£¾Ù¸öÀý×Ó£¬ÔÚ²¼¾ÖÖÐÓм¸¸öµØ·½ÒªÓð×É«»òÕß #ffffff ÑÕÉ«Öµ¡£²»ÒªÃ¿´Î¶¼Ö±½ÓÓà #ffffff ÊýÖµ£¬Ò²²»Òª×Ô¼ºÎª°×É«ÖØÐÂÉùÃ÷×ÊÔ´£¬ÎÒÃÇ¿ÉÒÔÖ±½ÓÓÃÕâ¸ö£¬
@android:color/white
°²×¿Ô¤Ïȶ¨ÒåÁ˺ܶೣÓõÄÑÕÉ«³£Á¿£¬±ÈÈç°×É«£¬ºÚÉ«»ò·ÛÉ«¡£×î¾µäµÄÓ¦Óó¡¾°ÊÇ͸Ã÷É«£º
@android:color/transparent
ÁíÒ»¸öÒýÓó£Á¿µÄ·½Ê½ÊÇ ?attr£¬ÓÃÀ´½«Ô¤Ïȶ¨ÒåµÄÊôÐÔÖµ¸³Öµ¸ø²»Í¬µÄÊôÐÔ¡£¾Ù¸ö×Ô¶¨Òå Toolbar µÄÀý×Ó¡£Õâ¸ö Toolbar ÐèÒª¶¨Òå¿í¶ÈºÍ¸ß¶È¡£¿í¶Èͨ³£¿ÉÒÔÉèÖÃΪ MATCH_PARENT£¬µ«¸ß¶ÈÄØ£¿ÎÒÃÇ´ó¶àÊýÈ˶¼Ã»ÓÐ×¢ÒâÉè¼ÆÖ¸µ¼£¬Ö»ÊǼòµ¥µØËæ±ãÉèÖÃÒ»¸ö¿´ÉÏÈ¥²î²»¶àµÄÖµ¡£ÕâÑù×ö²»¶Ô¡£²»Ó¦¸ÃËæ±ã×Ô¶¨Òå¸ß¶È£¬¶øÓ¦¸ÃÕâÑù×ö£¬
android:layout_height="?attr/actionBarSize"
?attr µÄÁíÒ»¸öÓ¦ÓÃÊǵã»÷ÊÓͼʱ»Ë®²¨ÎÆЧ¹û¡£SelectableItemBackground ÊÇÒ»¸öÔ¤¶¨ÒåµÄ drawable£¬ÈκÎÊÓͼÐèÒªÔö¼Ó²¨ÎÆЧ¹ûʱ¿ÉÒÔ½«ËüÉèΪ±³¾°£º
android:background="?attr/selectableItemBackground"
Ò²¿ÉÒÔÓÃÕâ¸ö£º
android:background="?attr/selectableItemBackgroundBorderless"
À´ÏÔʾÎޱ߿ò²¨ÎÆ¡£
4¡¢ SP ºÍ DP µÄÇø±ð
ËäÈ»ÕâÁ½¸öûÓб¾ÖÊÉϵÄÇø±ð£¬µ«ÖªµÀËüÃÇÊÇʲôÒÔ¼°ÔÚʲôµØ·½ÊʺÏÓÃÄĸöºÜÖØÒª¡£
SP µÄÒâ˼ÊÇËõ·ÅÎÞ¹ØÏñËØ£¬Ò»°ã½¨ÒéÓÃÓÚ TextView£¬Ê×ÏÈÎÄ×Ö²»»áÒòΪÏÔʾÃܶȲ»Í¬¶øÏÔʾЧ¹û²»Ò»Ñù£¬ÁíÍâ TextView µÄÄÚÈÝ»¹ÐèÒª¸ù¾ÝÓû§É趨×öÀÉ죬»òÕßÖ»µ÷Õû×ÖÌå´óС¡£
ÆäËûÐèÒª¶¨Òå³ß´çºÍλÖõĵط½£¬¿ÉÒÔʹÓà DP£¬Ò²¾ÍÊÇÃܶÈÎÞ¹ØÏñËØ¡£Ö®Ç°Ëµ¹ý£¬DP ºÍ SP µÄÐÔÖÊÊÇÒ»ÑùµÄ£¬Ö»ÊÇ DP »á¸ù¾ÝÏÔʾÃܶÈ×Ô¶¯ÀÉ죬ÒòΪ°²×¿ÏµÍ³»á¶¯Ì¬¼ÆËãʵ¼ÊÏÔʾµÄÏñËØ£¬ÕâÑù¾Í¿ÉÒÔÈÃʹÓà DP µÄ×é¼þÔÚ²»Í¬ÏÔʾÃܶȵÄÉ豸É϶¼¿ÉÒÔÓµÓÐÏàͬµÄÏÔʾЧ¹û¡£
5¡¢ Drawable ºÍ Mipmap µÄÓ¦ÓÃ
ÕâÁ½¸ö×îÈÃÈËÀ§»óµÄÊÇ - drawable ºÍ mipmap ÓжàÉÙ²îÒ죿
ËäÈ»ÕâÁ½¸öºÃÏñÓÐͬÑùµÄÓÃ;£¬µ«ËüÃÇÉè¼ÆÄ¿µÄ²»Ò»Ñù¡£mipmap ÊÇÓÃÀ´´¢´æͼ±êµÄ£¬¶ø drawable ÓÃÓÚÈκÎÆäËû¸ñʽ¡£ÎÒÃÇ¿ÉÒÔ¿´Ò»ÏÂϵͳÄÚ²¿ÊÇÈçºÎʹÓÃËüÃǵģ¬¾ÍÖªµÀΪʲô²»ÄÜ»ìÓÃÁË¡£
Äã¿ÉÒÔ¿´µ½ÄãµÄÓ¦ÓÃÀïÓм¸¸ö mipmap ºÍ drawable Ŀ¼£¬Ã¿Ò»¸ö·Ö±ð´ú±í²»Í¬µÄÏÔʾ·Ö±æÂÊ¡£µ±ÏµÍ³´Ó drawable Ŀ¼¶ÁÈ¡×ÊԴʱ£¬Ö»»á¸ù¾Ýµ±Ç°É豸µÄÏÔʾÃܶÈÑ¡ÔñÈ·¶¨µÄĿ¼¡£È»¶ø£¬ÔÚ¶ÁÈ¡ mipmap ʱ£¬ÏµÍ³»á¸ù¾ÝÐèҪѡÔñºÏÊʵÄĿ¼£¬¶ø²»½öÏÞÓÚµ±Ç°ÏÔʾÃܶȣ¬Ö÷ÒªÊÇÒòΪÓÐЩÆô¶¯Æ÷»á¹ÊÒâÏÔʾ½Ï´óµÄͼ±ê£¬ËùÒÔϵͳ»áʹÓýϴó·Ö±æÂʵÄ×ÊÔ´¡£
×ÜÖ®£¬Óà mipmap À´´æ·Åͼ±ê»ò±ê¼ÇͼƬ£¬¿ÉÒÔÔÚ²»Í¬ÏÔʾÃܶȵÄÉ豸ÉÏ¿´µ½·Ö±æÂʱ仯£¬¶øÆäËü¸ù¾ÝÐèÒªÏÔʾµÄͼƬ×ÊÔ´¶¼Óà drawable¡£
±ÈÈç˵£¬Nexus 5 µÄÏÔʾ·Ö±æÂÊÊÇ xxhdpi¡£µ±ÎÒÃÇ°Ñͼ±ê·Åµ½ mipmap Ŀ¼Àïʱ£¬ËùÓÐ mipmap Ŀ¼¶¼½«¶ÁÈëÄÚ´æ¡£¶øÈç¹û·Åµ½ drawable ÀֻÓÐ drawable-xxhdpi Ŀ¼»á±»¶ÁÈ¡£¬ÆäËûĿ¼¶¼»á±»ºöÂÔ¡£
6¡¢ ʹÓÃʸÁ¿Í¼ÐÎ
ΪÁËÖ§³Ö²»Í¬ÏÔʾÃܶȵÄÆÁÄ»£¬½«Í¬Ò»¸ö×ÊÔ´µÄ¶à¸ö°æ±¾£¨´óС£©Ìí¼Óµ½ÏîÄ¿ÀïÊÇÒ»¸öºÜ³£¼ûµÄ¼¼ÇÉ¡£ÕâÖÖ·½Ê½È·ÊµÓÐÓ㬲»¹ýËüÒ²»á´øÀ´Ò»¶¨µÄÐÔÄÜ¿ªÖ§£¬±ÈÈç¸ü´óµÄ apk ÎļþÒÔ¼°¶îÍâµÄ¿ª·¢¹¤×÷¡£ÎªÁËÏû³ýÕâÖÖÓ°Ï죬¹È¸èµÄ°²×¿ÍŶӷ¢²¼ÁËÐÂÔöµÄʸÁ¿Í¼ÐΡ£
ʸÁ¿Í¼ÐÎÊÇÓà XML ÃèÊöµÄ SVG£¨¿ÉÀÉìʸÁ¿Í¼ÐΣ©£¬ÊÇÓõ㡢ֱÏߺÍÇúÏß×éºÏÒÔ¼°Ìî³äÑÕÉ«»æÖƳöµÄͼÐΡ£ÕýÒòΪʸÁ¿Í¼ÐÎÊÇÓɵãºÍÏ߶¯Ì¬»³öÀ´µÄ£¬ÔÚ²»Í¬ÏÔʾÃܶÈÏÂÀÉìÒ²²»»áËðʧ·Ö±æÂÊ¡£¶øʸÁ¿Í¼ÐδøÀ´µÄÁíÒ»¸öºÃ´¦ÊǸüÈÝÒ××ö¶¯»¡£ÍùÒ»¸ö AnimatedVectorDrawable ÎļþÀïÌí¼Ó¶à¸öʸÁ¿Í¼ÐξͿÉÒÔ×ö³ö¶¯»£¬¶ø²»ÓÃÌí¼Ó¶àÕÅͼƬȻºóÔÙ·Ö±ð´¦Àí¡£ÉÏÃæµÄÏòÁ¿¶¨Òå¿ÉÒÔ»³öÏÂÃæµÄͼÐΣ¬
ÒªÔÚÄãµÄ°²×¿ÏîÄ¿ÀïÌí¼ÓʸÁ¿Í¼ÐΣ¬¿ÉÒÔÓÒ¼üµã»÷ÄãÏîÄ¿ÀïµÄÓ¦ÓÃÄ£¿é£¬È»ºóÑ¡Ôñ New >> Vector Assets¡£È»ºó»á´ò¿ª Assets Studio£¬Äã¿ÉÒÔÓÐÁ½ÖÖ·½Ê½Ìí¼ÓʸÁ¿Í¼ÐΡ£µÚÒ»ÖÖÊÇ´Ó Material ͼ±êÀïÑ¡Ôñ£¬ÁíÒ»ÖÖÊÇÑ¡Ôñ±¾µØµÄ SVG »ò PSD Îļþ¡£
¹È¸è½¨ÒéÓëÓ¦ÓÃÏà¹Ø¶¼Ê¹Óà Material ͼ±ê£¬À´±£³Ö°²×¿µÄÁ¬¹áÐÔºÍͳһÌåÑé¡£ÕâÀïÓÐÈ«²¿Í¼±ê£¬¼ÇµÃ¿´Ò»Ï¡£
7¡¢ É趨±ß½çµÄ¿ªÊ¼ºÍ½áÊø
ÕâÊÇÈËÃÇ×îÈÝÒ׺öÂԵĵط½Ö®Ò»¡£±ß½ç£¡Ôö¼Ó±ß½çµ±È»ºÜ¼òµ¥£¬µ«ÊÇÈç¹ûÒª¿¼ÂÇÖ§³ÖºÜ¾ÉµÄƽ̨ÄØ£¿
±ß½çµÄ¡°¿ªÊ¼¡±ºÍ¡°½áÊø¡±·Ö±ðÊÇ¡°×󡱺͡°ÓÒ¡±µÄ³¬¼¯£¬ËùÒÔÈç¹ûÓ¦ÓÃµÄ minSdkVersion ÊÇ 17 »ò¸üµÍ£¬±ß½çºÍÌî³äµÄ¡°¿ªÊ¼¡±ºÍ¡°½áÊø¡±¶¨ÒåÊǾɵġ°×ó¡±/¡°ÓÒ¡±ËùÐèÒªµÄ¡£ÔÚÄÇЩûÓж¨Òå¡°¿ªÊ¼¡±ºÍ¡°½áÊø¡±µÄϵͳÉÏ£¬ÕâÁ½¸ö¶¨Òå¿ÉÒÔ±»°²È«µØºöÂÔ¡£¿ÉÒÔÏñÏÂÃæÕâÑùÉùÃ÷£º
android:layout_marginEnd="20dp" android:paddingStart="20dp"
8¡¢ ʹÓà Getter/Setter Éú³É¹¤¾ß
ÔÚ´´½¨Ò»¸öÈÝÆ÷ÀֻࣨÊÇÓÃÀ´¼òµ¥µÄ´æ·ÅһЩ±äÁ¿Êý¾Ý£©Ê±ºÜ·³µÄÒ»¼þÊÂÇéÊÇд¶à¸ö getter ºÍ setter£¬¸´ÖÆ/Õ³Ìù¸Ã·½·¨µÄÖ÷ÌåÔÙΪÿ¸ö±äÁ¿ÖØÃüÃû¡£
ÐÒÔ˵ÄÊÇ£¬Android Studio ÓÐÒ»¸ö½â¾ö·½·¨¡£¿ÉÒÔÕâÑù×ö£¬ÔÚÀàÀïÉùÃ÷ÄãÐèÒªµÄËùÓбäÁ¿£¬È»ºó´ò¿ª Toolbar >> Code¡£¿ì½Ý·½Ê½ÊÇ ALT + Insert¡£µã»÷ Code »áÏÔʾ Generate£¬µã»÷Ëü»á³öÀ´ºÜ¶àÑ¡ÏÀïÃæÓÐ Getter ºÍ Setter Ñ¡Ïî¡£ÔÚ±£³Ö½¹µãÔÚÄãµÄÀàÒ³ÃæÈ»ºóµã»÷£¬¾Í»áΪµ±Ç°ÀàÌí¼ÓËùÓÐµÄ getter ºÍ setter£¨ÓÐÐèÒªµÄ»°¿ÉÒÔÔÙȥ֮ǰµÄ´°¿Ú²Ù×÷£©¡£ºÜˬ°É¡£
9¡¢ ʹÓà Override/Implement Éú³É¹¤¾ß
ÕâÊÇÁíÒ»¸öºÜºÃÓõÄÉú³É¹¤¾ß¡£×Ô¶¨ÒåÒ»¸öÀàÈ»ºóÔÙÀ©Õ¹ºÜÈÝÒ×£¬µ«ÊÇÈç¹ûÒªÀ©Õ¹Äã²»ÊìϤµÄÀàÄØ¡£±ÈÈç˵ PagerAdapter£¬ÄãÏ£ÍûÓà ViewPager À´Õ¹Ê¾Ò»Ð©Ò³Ã棬ÄǾÍÐèÒª¶¨ÖÆÒ»¸ö PagerAdapter ²¢ÊµÏÖËüµÄÖØÔØ·½·¨¡£µ«ÊǾßÌåÓÐÄÄЩ·½·¨ÄØ£¿Android Studio ·Ç³£ÌùÐĵØΪ×Ô¶¨ÒåÀàÇ¿ÐÐÌí¼ÓÁËÒ»¸ö¹¹Ô캯Êý£¬»òÕß¿ÉÒÔÓÿì½Ý¼ü£¨ALT + Enter£©£¬µ«ÊǸ¸Àà PagerAdapter ÀïµÄÆäËû£¨ÐéÄ⣩·½·¨ÐèÒª×Ô¼ºÊÖ¶¯Ìí¼Ó£¬ÎÒ¹À¼Æ´ó¶àÊýÈ˶¼¾õµÃ·³¡£
ÒªÁгöËùÓпÉÒÔÖØÔصķ½·¨£¬¿ÉÒÔµã»÷ Code >> Generate and Override methods »òÕß Implement methods£¬¸ù¾ÝÄãµÄÐèÒª¡£Ä㻹¿ÉÒÔΪÄãµÄÀàÑ¡Ôñ¶à¸ö·½·¨£¬Ö»Òª°´×¡ Ctrl ÔÙÑ¡Ôñ·½·¨£¬È»ºóµã»÷ OK¡£
10¡¢ ÕýÈ·Àí½â Context
Context Óеã¿Ö²À£¬ÎÒ¹À¼ÆÐí¶à³õѧÕß´ÓûÓÐÈÏÕæÀí½â¹ý Context ÀàµÄ½á¹¹ - ËüÊÇʲô£¬ÎªÊ²Ã´µ½´¦¶¼ÒªÓõ½Ëü¡£
¼òµ¥µØ˵£¬Ëü½«ÄãÄÜ´ÓÆÁÄ»ÉÏ¿´µ½µÄËùÓÐÄÚÈݶ¼ÕûºÏÔÚÒ»Æð¡£ËùÓеÄÊÓͼ£¨»òÕßËüÃǵÄÀ©Õ¹£©¶¼Í¨¹ý Context °ó¶¨µ½µ±Ç°µÄ»·¾³¡£Context ÓÃÀ´¹ÜÀíÓ¦Óòã´ÎµÄ×ÊÔ´£¬±ÈÈç˵ÏÔʾÃܶȣ¬»òÕßµ±Ç°µÄ¹ØÁª»î¶¯¡£»î¶¯¡¢·þÎñºÍÓ¦Óö¼ÊµÏÖÁË Context ÀàµÄ½Ó¿ÚÀ´ÎªÆäËû¹ØÁª×é¼þÌṩÄÚ²¿×ÊÔ´¡£¾Ù¸öÌí¼Óµ½ MainActivity µÄ TextView µÄÀý×Ó¡£ÄãÓ¦¸Ã×¢Òâµ½ÁË£¬ÔÚ´´½¨Ò»¸ö¶ÔÏóµÄʱºò£¬TextView µÄ¹¹Ô캯ÊýÐèÒª Context ²ÎÊý¡£ÕâÊÇΪÁË»ñÈ¡ TextView ÀﶨÒåµ½µÄ×ÊÔ´¡£±ÈÈç˵£¬TextView ÐèÒªÔÚÄÚ²¿Óõ½ Roboto ×ÖÌå¡£ÕâÑùµÄ»°£¬TextView ÐèÒª Context¡£¶øÇÒÔÚÎÒÃǽ« Context£¨»òÕß this£©´«µÝ¸ø TextView µÄʱºò£¬Ò²¾ÍÊǸæËßËü°ó¶¨µ±Ç°»î¶¯µÄÉúÃüÖÜÆÚ¡£
ÁíÒ»¸ö Context µÄ¹Ø¼üÓ¦ÓÃÊdzõʼ»¯Ó¦Óòã´ÎµÄ²Ù×÷£¬±ÈÈç³õʼ»¯Ò»¸ö¿â¡£¿âµÄÉúÃüÖÜÆÚºÍÓ¦ÓÃÊDz»Ïà¹ØµÄ£¬ËùÒÔËüÐèÒªÓà getApplicationContext() À´³õʼ»¯£¬¶ø²»ÊÇÓà getContext »ò this »ò getActivity()¡£ÕÆÎÕÕýȷʹÓò»Í¬ Context ÀàÐͷdz£ÖØÒª£¬¿ÉÒÔ±ÜÃâÄÚ´æй©¡£ÁíÍ⣬ҪÓõ½ Context À´Æô¶¯Ò»¸ö»î¶¯»ò·þÎñ¡£»¹¼ÇµÃ startActivity(¡) Â𣿵±ÄãÐèÒªÔÚÒ»¸ö·Ç»î¶¯ÀàÀïÇл»»î¶¯Ê±£¬ÄãÐèÒªÒ»¸ö Context ¶ÔÏóÀ´µ÷Óà startActivity ·½·¨£¬ÒòΪËüÊÇ Context ÀàµÄ·½·¨£¬¶ø²»ÊÇ Activity Àà¡£
getContext().startActivity(getContext(), SecondActivity.class);
Èç¹ûÄãÏëÁ˽â¸ü¶à Context µÄÐÐΪ£¬¿ÉÒÔ¿´¿´ÕâÀï»òÕâÀï¡£µÚÒ»¸öÊÇһƪ¹ØÓÚ Context µÄºÜºÃµÄÎÄÕ£¬½éÉÜÁËÔÚÄÄЩµØ·½ÒªÓõ½Ëü¡£¶øÁíÒ»¸öÊÇ°²×¿¹ØÓÚ Context µÄÎĵµ£¬È«Ãæ½éÉÜÁËËùÓеŦÄÜ - ·½·¨£¬¾²Ì¬±êʶÒÔ¼°¸ü¶à¡£
½±Àø #1: ¸ñʽ»¯´úÂë
ÓÐÈ˻᲻ϲ»¶ÕûÆ룬ͳһ¸ñʽµÄ´úÂëÂ𣿺ðɣ¬¼¸ºõÎÒÃÇÿһ¸öÈË£¬ÔÚдһ¸ö³¬¹ý 1000 ÐеÄÀàµÄʱºò£¬¶¼Ï£ÍûÎÒÃǵĴúÂëÄÜÓкÏÊʵĽṹ¡£¶øÇÒ£¬²¢²»½ö½ö´óµÄÀà²ÅÐèÒª¸ñʽ»¯£¬Ã¿Ò»¸öСģ¿éÀàÒ²ÐèÒªÈôúÂë±£³Ö¿É¶ÁÐÔ¡£
ʹÓà Android Studio£¬»òÕßÈκΠJetBrains IDE£¬Ä㶼²»ÐèÒª×Ô¼ºÊÖ¶¯ÕûÀíÄãµÄ´úÂ룬ÏñÔö¼ÓËõ½ø»òÕß = ֮ǰµÄ¿Õ¸ñ¡£¾Í°´×Ô¼ºÏ£ÍûµÄ·½Ê½Ð´´úÂ룬ÔÚÏëÒª¸ñʽ»¯µÄʱºò£¬Èç¹ûÊÇ Windows ϵͳ¿ÉÒÔ°´Ï ALT + CTRL + L£¬Linux ϵͳ°´Ï ALT + CTRL + SHIFT + L¡£´úÂë¾Í×Ô¶¯¸ñʽ»¯ºÃÁË
½±Àø #2: ʹÓÿâ
ÃæÏò¶ÔÏó±à³ÌµÄÒ»¸öÖØÒªÔÔòÊÇÔö¼Ó´úÂëµÄ¿ÉÖØÓÃÐÔ£¬»òÕß˵¼õÉÙÖØз¢Ã÷ÂÖ×ÓµÄÏ°¹ß¡£ºÜ¶à³õѧÕß´íÎóµØ×ñÑÁËÕâ¸öÔÔò¡£ÕâÌõ·ÓÐÁ½¸ö·½Ïò£¬
²»ÓÃÈκο⣬×Ô¼ºÐ´ËùÓеĴúÂë¡£ ÓÿâÀ´´¦ÀíËùÓÐÊÂÇé¡£
²»¹ÜÄĸö·½Ïò×ßµ½µ×¶¼ÊDz»¶ÔµÄ¡£Èç¹ûÄã³¹µ×Ñ¡ÔñµÚÒ»¸ö·½Ïò£¬Ä㽫ÏûºÄ´óÁ¿µÄ×ÊÔ´£¬½ö½öÊÇΪÁËÂú×ã×Ô¼ºÓµÓÐÒ»ÇеĽ¾°Á¡£ºÜ¿ÉÄÜÄãµÄ´úÂëûÓÐ×ö¹ýÌæ´ú¿âÄÇô¶àµÄ²âÊÔ£¬´Ó¶øÔö¼ÓÄ£¿é³öÎÊÌâµÄ¿ÉÄÜ¡£Èç¹û×ÊÔ´ÓÐÏÞ£¬²»ÒªÖظ´·¢Ã÷ÂÖ×Ó¡£Ö±½ÓÓþ¹ý²âÊԵĿ⣬ÔÚÓÐÁËÃ÷È·Ä¿±êÒÔ¼°³ä·ÖµÄ×ÊÔ´ºó£¬¿ÉÒÔÓÃ×Ô¼ºµÄ¿É¿¿´úÂëÀ´Ìæ»»Õâ¸ö¿â¡£
¶ø³¹µ××ßÏòÁíÒ»¸ö·½Ïò£¬ÎÊÌâ¸üÑÏÖØ - ±ðÈË´úÂëµÄ¿É¿¿ÐÔ¡£²»ÒªÏ°¹ßÓÚËùÓÐÊÂÇ鶼ÒÀÀµÓÚ±ðÈ˵ĴúÂë¡£ÔÚ²»ÓÃÌ«¶à×ÊÔ´»òÕß×Ô¼ºÄÜÕƿصÄÇé¿öϾ¡Á¿×Ô¼ºÐ´´úÂë¡£Äã²»ÐèÒªÓÿâÀ´×Ô¶¨ÒåÒ»¸ö TypeFaces£¨×ÖÌ壩£¬Äã¿ÉÒÔ×Ô¼ºÐ´Ò»¸ö¡£
ËùÒÔÒª¼Çס£¬ÔÚÕâÁ½¸ö¼«¶ËÖмäƽºâһϠ- ²»ÒªÖØд´ÔìËùÓÐÊÂÇ飬Ҳ²»Òª¹ý·ÖÒÀÀµÍⲿ´úÂë¡£±£³ÖÖÐÁ¢£¬¸ù¾Ý×Ô¼ºµÄÄÜÁ¦Ð´´úÂë¡£
ÕâƪÎÄÕÂ×îÔç·¢²¼ÔÚ What¡¯s That Lambda ÉÏ¡£Çë·ÃÎÊÍøÕ¾ÔĶÁ¸ü¶à¹ØÓÚ Android¡¢Node.js¡¢Angular.js µÈµÈÀàËÆÎÄÕ¡£
±¾ÎÄתÔØ×Ô£ºhttp://www.linuxprobe.com/android-12tips.html
Ãâ·ÑÌṩ×îÐÂLinux¼¼Êõ½Ì³ÌÊé¼®£¬Îª¿ªÔ´¼¼Êõ°®ºÃÕßŬÁ¦×öµÃ¸ü¶à¸üºÃ£¬¿ªÔ´Õ¾µã£ºhttp://www.linuxprobe.com/
Ronny ÓÚ 2017-11-13 12:50:30·¢±í:
Êʺϰ²×¿¿ª·¢³õѧÕßµÄ12¸ö¼¼ÇÉ