当前位置: 代码迷 >> Android >> Android涓?皢甯冨眬鏂囦欢/View娣诲姞鑷崇獥鍙h繃绋嬪垎鏋? 浠巗etContentView()璋堣捣
  详细解决方案

Android涓?皢甯冨眬鏂囦欢/View娣诲姞鑷崇獥鍙h繃绋嬪垎鏋? 浠巗etContentView()璋堣捣

热度:409   发布时间:2016-05-01 10:21:38.0
Android涓?皢甯冨眬鏂囦欢/View娣诲姞鑷崇獥鍙h繃绋嬪垎鏋?---- 浠巗etContentView()璋堣捣

聽 聽聽杞?浇璇锋敞鏄庡嚭澶勶細http://blog.csdn.net/qinjuning聽聽聽聽聽

聽聽聽聽聽聽聽聽 鏈?枃涓昏?鍐呭?鏄??瑙d竴涓??鍥綱iew鎴栬?呬竴涓猇iewGroup瀵硅薄鏄??浣曟坊鍔犺嚦搴旂敤绋嬪簭绐楀彛涓?殑銆?/span>

聽聽聽聽聽聽聽 涓嬫枃涓?彁鍒扮殑绐楀彛鍙?硾鎸?/span>鎴?/span>浠?/span>鑳界湅鍒扮殑鐣岄潰锛屽寘鎷?竴涓狝ctivity鍛堢幇鐨勭晫闈?鎴戜滑鍙?互灏嗕箣鐞嗚В涓哄簲鐢ㄧ▼搴忕獥鍙?锛屼竴涓狣ialog锛?/span>

聽聽 涓?涓猅oast锛屼竴涓狹enu鑿滃崟绛夈??/span>

聽聽聽聽聽 棣栧厛瀵圭浉鍏崇被鐨勪綔鐢ㄨ繘琛屼竴涓嬬畝鍗曚粙缁嶏細

聽聽聽聽聽聽聽聽Window 绫?/span>聽聽 浣嶄簬 /frameworks/base/core/java/android/view/Window.java

聽聽聽聽聽聽聽聽聽聽聽 璇存槑锛氳?绫绘槸涓?涓?娊璞$被锛屾彁渚涗簡缁樺埗绐楀彛鐨勪竴缁勯?氱敤API銆傚彲浠ュ皢涔嬬悊瑙d负涓?涓?浇浣擄紝鍚勭?View鍦ㄨ繖涓?浇浣撲笂鏄剧ず銆?/span>

聽聽聽聽聽聽聽聽聽聽 聽 婧愭枃浠?閮ㄥ垎)濡備笅锛?/span>

[java]聽view plaincopyprint?
  1. publicabstractclass聽Window聽{聽聽聽聽
  2. 聽聽聽聽//...聽聽
  3. 聽聽聽聽//鎸囧畾Activity绐楀彛鐨勯?鏍肩被鍨?/span>聽聽
  4. 聽聽聽聽publicstaticfinalint聽FEATURE_NO_TITLE聽=聽1;聽聽
  5. 聽聽聽聽publicstaticfinalint聽FEATURE_INDETERMINATE_PROGRESS聽=聽5;聽聽
  6. 聽聽聽聽聽聽
  7. 聽聽聽聽//璁剧疆甯冨眬鏂囦欢聽聽
  8. 聽聽聽聽publicabstractvoid聽setContentView(int聽layoutResID);聽聽
  9. 聽聽
  10. 聽聽聽聽publicabstractvoid聽setContentView(View聽view);聽聽
  11. 聽聽
  12. 聽聽聽聽//璇锋眰鎸囧畾Activity绐楀彛鐨勯?鏍肩被鍨?/span>聽聽
  13. 聽聽聽聽publicboolean聽requestFeature(int聽featureId)聽{聽聽
  14. 聽聽聽聽聽聽聽聽finalint聽flag聽=聽1<<featureId;聽聽
  15. 聽聽聽聽聽聽聽聽mFeatures聽|=聽flag;聽聽
  16. 聽聽聽聽聽聽聽聽mLocalFeatures聽|=聽mContainer聽!=聽null聽?聽(flag&~mContainer.mFeatures)聽:聽flag;聽聽
  17. 聽聽聽聽聽聽聽聽return聽(mFeatures&flag)聽!=聽0;聽聽
  18. 聽聽聽聽}聽聽聽聽聽聽
  19. 聽聽聽聽//...聽聽
  20. }聽聽

聽聽聽聽聽聽 PhoneWindow绫?/span>聽 浣嶄簬/frameworks/policies/base/phone/com/android/internal/policy/impl/PhoneWindow.java

聽聽聽聽聽聽聽聽 璇存槑锛?璇ョ被缁ф壙浜嶹indow绫伙紝鏄疻indow绫荤殑鍏蜂綋瀹炵幇锛屽嵆鎴戜滑鍙?互閫氳繃璇ョ被鍏蜂綋鍘荤粯鍒剁獥鍙c?傚苟涓旓紝璇ョ被鍐呴儴鍖呭惈浜?/span>

聽聽聽聽聽聽聽聽聽聽聽 涓?涓狣ecorView瀵硅薄锛岃?DectorView瀵硅薄鏄?墍鏈夊簲鐢ㄧ獥鍙?Activity鐣岄潰)鐨勬牴View銆?绠?鑰岃█涔嬶紝PhoneWindow绫绘槸

聽聽聽聽聽聽聽聽聽聽聽 鎶婁竴涓狥rameLayout绫诲嵆DecorView瀵硅薄杩涜?涓?瀹氱殑鍖呰?锛屽皢瀹冧綔涓哄簲鐢ㄧ獥鍙g殑鏍筕iew锛屽苟鎻愪緵涓?缁勯?氱敤鐨勭獥鍙f搷浣?/span>

聽聽聽聽聽聽聽聽聽聽聽 鎺ュ彛銆?/span>

聽聽聽聽聽聽聽聽聽聽聽聽聽 聽婧愭枃浠?閮ㄥ垎)濡備笅锛?/span>聽聽聽聽聽聽聽聽聽聽

[java]聽view plaincopyprint?
  1. publicclass聽PhoneWindow聽extends聽Window聽implements聽MenuBuilder.Callback聽{聽聽
  2. 聽聽聽聽//...聽聽
  3. 聽聽聽聽//聽This聽is聽the聽top-level聽view聽of聽the聽window,聽containing聽the聽window聽decor.聽聽
  4. 聽聽聽聽private聽DecorView聽mDecor;聽聽//璇ュ?璞℃槸鎵?鏈夊簲鐢ㄧ獥鍙g殑鏍硅?鍥韭狅紝聽鏄疐rameLayout鐨勫瓙绫?/span>聽聽
  5. 聽聽聽聽聽聽
  6. 聽聽聽聽//璇ュ?璞℃槸Activity甯冨眬鏂囦欢鐨勭埗瑙嗗浘锛屼竴鑸?潵璇存槸涓?涓狥rameLayout鍨嬬殑ViewGroup聽聽聽
  7. 聽聽聽聽//聽鍚屾椂涔熸槸DecorView瀵硅薄鐨勪竴涓?瓙瑙嗗浘聽聽
  8. 聽聽聽聽//聽This聽is聽the聽view聽in聽which聽the聽window聽contents聽are聽placed.聽It聽is聽either聽聽
  9. 聽聽聽聽//聽mDecor聽itself,聽or聽a聽child聽of聽mDecor聽where聽the聽contents聽go.聽聽
  10. 聽聽聽聽private聽ViewGroup聽mContentParent;聽聽聽
  11. 聽聽聽聽聽聽
  12. 聽聽聽聽//璁剧疆鏍囬?聽聽
  13. 聽聽聽聽@Override聽聽
  14. 聽聽聽聽publicvoid聽setTitle(CharSequence聽title)聽{聽聽
  15. 聽聽聽聽聽聽聽聽if聽(mTitleView聽!=聽null)聽{聽聽
  16. 聽聽聽聽聽聽聽聽聽聽聽聽mTitleView.setText(title);聽聽
  17. 聽聽聽聽聽聽聽聽}聽聽
  18. 聽聽聽聽聽聽聽聽mTitle聽=聽title;聽聽
  19. 聽聽聽聽}聽聽
  20. 聽聽聽聽//璁剧疆鑳屾櫙鍥剧墖聽聽
  21. 聽聽聽聽@Override聽聽
  22. 聽聽聽聽publicfinalvoid聽setBackgroundDrawable(Drawable聽drawable)聽{聽聽
  23. 聽聽聽聽聽聽聽聽if聽(drawable聽!=聽mBackgroundDrawable聽||聽mBackgroundResource聽!=聽0)聽{聽聽
  24. 聽聽聽聽聽聽聽聽聽聽聽聽mBackgroundResource聽=聽0;聽聽
  25. 聽聽聽聽聽聽聽聽聽聽聽聽mBackgroundDrawable聽=聽drawable;聽聽
  26. 聽聽聽聽聽聽聽聽聽聽聽聽if聽(mDecor聽!=聽null)聽{聽聽
  27. 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽mDecor.setWindowBackground(drawable);聽聽
  28. 聽聽聽聽聽聽聽聽聽聽聽聽}聽聽
  29. 聽聽聽聽聽聽聽聽}聽聽
  30. 聽聽聽聽}聽聽
  31. 聽聽聽聽//...聽聽聽聽聽聽
  32. }聽聽

聽聽聽聽聽聽 DecorView绫?/strong>聽聽聽 璇ョ被鏄疨honeWindow绫荤殑鍐呴儴绫?/span>

聽聽聽聽聽聽聽聽 璇存槑锛?璇ョ被鏄?竴涓狥rameLayout鐨勫瓙绫伙紝骞朵笖鏄疨honeWindow鐨勫瓙绫伙紝璇ョ被灏辨槸瀵规櫘閫氱殑FrameLayout杩涜?鍔熻兘鐨勬墿灞曪紝

聽聽聽聽聽聽聽聽聽聽聽 鏇寸‘鍒囩偣鍙?互璇存槸淇?グ(Decor鐨勮嫳鏂囧叏绉版槸Decoration锛屽嵆鈥滀慨楗扳?濈殑鎰忔??锛屾瘮濡傝?娣诲姞TitleBar(鏍囬?鏍?锛屼互鍙?/span>

聽聽聽聽聽聽聽聽聽聽聽 TitleBar涓婄殑婊氬姩鏉$瓑 銆?span style="color: #cc0000;">鏈?閲嶈?鐨勪竴鐐规槸锛屽畠鏄?墍鏈夊簲鐢ㄧ獥鍙g殑鏍筕iew 銆?/strong>

聽聽聽聽聽聽聽聽 濡備笅鎵?绀?锛?/span>

聽聽聽

聽聽聽聽聽聽聽聽聽聽聽聽 聽聽

聽聽聽聽聽聽 聽聽聽聽DecorView 鏍硅?鍥剧粨鏋劼犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅犅?DecorView 鏍硅?鍥惧舰寮?/strong>

聽聽聽聽聽

聽聽聽聽 婧愭枃浠?閮ㄥ垎)濡備笅锛?/span>

[java]聽view plaincopyprint?
  1. privatefinalclass聽DecorView聽extends聽FrameLayout聽{聽聽
  2. 聽聽聽聽//...聽聽
  3. 聽聽聽聽//瑙︽懜浜嬩欢澶勭悊聽聽
  4. 聽聽聽聽@Override聽聽
  5. 聽聽聽聽publicboolean聽onTouchEvent(MotionEvent聽event)聽{聽聽
  6. 聽聽聽聽聽聽聽聽return聽onInterceptTouchEvent(event);聽聽
  7. 聽聽聽聽}聽聽
  8. 聽聽聽聽//...聽聽
  9. }聽聽


聽聽聽聽 聽 鎵撲釜涓嶆伆褰撴瘮鍠诲惂锛學indow绫荤浉褰撲簬涓?骞呯敾(鎶借薄姒傚康锛屼粈涔堢敾鎴戜滑鏈?煡) 锛孭honeWindow涓轰竴鍓?綈鐧界煶鍏堢敓鐨勫北姘寸敾

聽聽 (鍏蜂綋姒傚康锛屾垜浠?煡閬撲簡鏄?皝鐨勩?佷粈涔堟?ц川鐨勭敾)锛孌ecorView鍒欎负璇ュ北姘寸敾鐨勫叿浣撳唴瀹?鏈夊北銆佹湁姘淬?佹湁鏍戯紝鍚勭?鐣岄潰)銆?/span>

聽聽 DecorView鍛堢幇鍦≒honeWindow涓娿??/span>

聽聽聽聽聽聽 褰撶郴缁?涓?鑸?槸ActivityManagerService)閰嶇疆濂藉惎鍔ㄤ竴涓狝ctivity鐨勭浉鍏冲弬鏁?鍖呮嫭Activity瀵硅薄鍜學indow瀵硅薄淇℃伅)鍚庯紝

聽聽 灏变細鍥炶皟Activity鐨刼nCreate()鏂规硶锛屽湪鍏朵腑鎴戜滑閫氳繃璁剧疆setContentView()鏂规硶绫昏?缃??Activity鐨勬樉绀虹晫闈?紝鏁翠釜璋冪敤閾?/span>

聽聽 鐢辨?閾哄灚寮?鏉ャ?俿etContentView()鐨勪笁涓?瀯閫犳柟娉曡皟鐢ㄦ祦绋嬫湰璐ㄤ笂鏄?竴鏍风殑锛屾垜浠?氨鍒嗘瀽setContentView(intresId)鏂规硶銆?/span>

聽聽

聽 聽聽

聽 聽 聽 聽 聽 聽

Step 1聽聽銆丄ctivity.setContentView(intresId)聽聽 璇ユ柟娉曞湪Activity绫讳腑

聽聽聽聽聽聽聽聽聽璇ユ柟娉曞彧鏄?畝鍗曠殑鍥炶皟Window瀵硅薄锛屽叿浣撲负PhoneWindow瀵硅薄鐨剆etContentView()鏂规硶瀹炵幇 銆?/span>

[java]聽view plaincopyprint?
  1. publicvoid聽setContentView(int聽layoutResID)聽{聽聽
  2. 聽聽聽聽getWindow().setContentView(layoutResID);聽聽
  3. }聽聽
  4. 聽聽
  5. public聽Window聽getWindow()聽{聽聽
  6. 聽聽聽聽return聽mWindow;聽聽聽//Window瀵硅薄锛屾湰璐ㄤ笂鏄?竴涓狿honeWindow瀵硅薄聽聽
  7. }聽聽


聽Step 2聽聽銆丳honeWindow.setContentView()聽聽聽聽璇ユ柟娉曞湪PhoneWindow绫讳腑聽

[java]聽view plaincopyprint?
  1. @Override聽聽
  2. publicvoid聽setContentView(int聽layoutResID)聽{聽聽
  3. 聽聽聽聽//鏄?惁鏄??涓?娆¤皟鐢╯etContentView鏂规硶锛屄犲?鏋滄槸绗?竴娆¤皟鐢?紝鍒檓Decor鍜宮ContentParent瀵硅薄閮戒负绌?/span>聽聽
  4. 聽聽聽聽if聽(mContentParent聽==聽null)聽{聽聽
  5. 聽聽聽聽聽聽聽聽installDecor();聽聽
  6. 聽聽聽聽}聽else聽{聽聽
  7. 聽聽聽聽聽聽聽聽mContentParent.removeAllViews();聽聽
  8. 聽聽聽聽}聽聽
  9. 聽聽聽聽mLayoutInflater.inflate(layoutResID,聽mContentParent);聽聽
  10. 聽聽聽聽final聽Callback聽cb聽=聽getCallback();聽聽
  11. 聽聽聽聽if聽(cb聽!=聽null)聽{聽聽
  12. 聽聽聽聽聽聽聽聽cb.onContentChanged();聽聽
  13. 聽聽聽聽}聽聽
  14. }聽聽

聽 聽 聽 聽璇ユ柟娉曟牴鎹??鍏堝垽鏂?槸鍚﹀凡缁忕敱setContentView()浜嗚幏鍙杕ContentParent鍗砎iew瀵硅薄锛?鍗虫槸鍚︽槸绗?竴娆¤皟鐢ㄨ?

聽聽聽PhoneWindow瀵硅薄setContentView()鏂规硶銆傚?鏋滄槸绗?竴娆¤皟鐢?紝鍒欒皟鐢╥nstallDecor()鏂规硶锛屽惁鍒欙紝绉婚櫎璇?ContentParent鍐?/span>

聽 聽鎵?鏈夌殑鎵?鏈夊瓙View銆傛渶鍚庡皢鎴戜滑鐨勮祫婧愭枃浠堕?氳繃LayoutInflater瀵硅薄杞?崲涓篤iew鏍戯紝骞朵笖娣诲姞鑷砿ContentParent瑙嗗浘涓???/span>

聽聽聽聽聽聽

聽聽聽聽聽聽聽PS锛氬洜姝わ紝鍦ㄥ簲鐢ㄧ▼搴忛噷锛屾垜浠?彲浠ュ?娆¤皟鐢╯etContentView()鏉ユ樉绀烘垜浠?殑鐣岄潰銆?/span>

聽Step 3銆?PhoneWindow. installDecor()聽聽聽 璇ユ柟娉曞湪PhoneWindow绫讳腑

[java]聽view plaincopyprint?
  1. privatevoid聽installDecor()聽{聽聽
  2. 聽聽聽聽if聽(mDecor聽==聽null)聽{聽聽
  3. 聽聽聽聽聽聽聽聽//mDecor涓虹┖锛屽垯鍒涘缓涓?涓狣ecor瀵硅薄聽聽
  4. 聽聽聽聽聽聽聽聽mDecor聽=聽generateDecor();聽聽
  5. 聽聽聽聽聽聽聽聽mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);聽聽
  6. 聽聽聽聽聽聽聽聽mDecor.setIsRootNamespace(true);聽聽
  7. 聽聽聽聽}聽聽
  8. 聽聽聽聽if聽(mContentParent聽==聽null)聽{聽聽
  9. 聽聽聽聽聽聽聽聽//generateLayout()鏂规硶浼氭牴鎹?獥鍙g殑椋庢牸淇?グ锛岄?夋嫨瀵瑰簲鐨勪慨楗板竷灞?鏂囦欢聽聽
  10. 聽聽聽聽聽聽聽聽//骞朵笖灏唅d涓篶ontent(android:id="@+id/content")鐨凢rameLayout璧嬪?肩粰mContentParent聽聽
  11. 聽聽聽聽聽聽聽聽mContentParent聽=聽generateLayout(mDecor);聽聽
  12. 聽聽聽聽聽聽聽聽聽聽
  13. 聽聽聽聽聽聽聽聽//...聽聽
  14. }聽聽

聽 聽棣栧厛銆佽?鏂规硶棣栧厛鍒ゆ柇mDecor瀵硅薄鏄?惁涓虹┖锛屽?鏋滀笉涓虹┖锛屽垯璋冪敤generateDecor()鍒涘缓涓?涓狣ecorView(璇ョ被鏄?/span>

聽聽聽聽聽聽聽聽聽聽 FrameLayout瀛愮被锛屽嵆涓?涓猇iewGroup瑙嗗浘) 锛?/span>

聽 聽 聽 generateDecor()鏂规硶鍘熷瀷涓?

[java]聽view plaincopyprint?
  1. protected聽DecorView聽generateDecor()聽{聽聽
  2. 聽聽聽聽returnnew聽DecorView(getContext(),聽-1);聽聽
  3. }聽聽

聽 鍏舵?銆佺户缁?垽鏂璵ContentParent瀵硅薄鏄?惁涓虹┖锛屽?鏋滀笉涓虹┖锛屽垯璋冪敤generateLayout()鏂规硶鍘诲垱寤簃ContentParent瀵硅薄銆?/span>

聽 聽 聽 聽 聽generateLayout()鏂规硶濡備笅锛?/span>

[java]聽view plaincopyprint?
  1. protected聽ViewGroup聽generateLayout(DecorView聽decor)聽{聽聽
  2. 聽聽聽聽//聽Apply聽data聽from聽current聽theme.聽聽
  3. 聽聽
  4. 聽聽聽聽//...1銆佹牴鎹畆equestFreature()鍜孉ctivity鑺傜偣鐨刟ndroid:theme=""聽璁剧疆濂铰爁eatures鍊?/span>聽聽
  5. 聽聽聽聽聽聽
  6. 聽聽聽聽//2聽鏍规嵁璁惧畾濂界殑聽features鍊硷紝鍗崇壒瀹氶?鏍煎睘鎬э紝閫夋嫨涓嶅悓鐨勭獥鍙d慨楗板竷灞?鏂囦欢聽聽
  7. 聽聽聽聽int聽layoutResource;聽聽//绐楀彛淇?グ甯冨眬鏂囦欢聽聽聽聽
  8. 聽聽聽聽int聽features聽=聽getLocalFeatures();聽聽
  9. 聽聽聽聽//聽System.out.println("Features:聽0x"聽+聽Integer.toHexString(features));聽聽
  10. 聽聽聽聽if聽((features聽&聽((1聽<<聽FEATURE_LEFT_ICON)聽|聽(1聽<<聽FEATURE_RIGHT_ICON)))聽!=聽0)聽{聽聽
  11. 聽聽聽聽聽聽聽聽if聽(mIsFloating)聽{聽聽
  12. 聽聽聽聽聽聽聽聽聽聽聽聽layoutResource聽=聽com.android.internal.R.layout.dialog_title_icons;聽聽
  13. 聽聽聽聽聽聽聽聽}聽else聽{聽聽
  14. 聽聽聽聽聽聽聽聽聽聽聽聽layoutResource聽=聽com.android.internal.R.layout.screen_title_icons;聽聽
  15. 聽聽聽聽聽聽聽聽}聽聽
  16. 聽聽聽聽聽聽聽聽//聽System.out.println("Title聽Icons!");聽聽
  17. 聽聽聽聽}聽elseif聽((features聽&聽((1聽<<聽FEATURE_PROGRESS)聽|聽(1聽<<聽FEATURE_INDETERMINATE_PROGRESS)))聽!=聽0)聽{聽聽
  18. 聽聽聽聽聽聽聽聽//聽Special聽case聽for聽a聽window聽with聽only聽a聽progress聽bar聽(and聽title).聽聽
  19. 聽聽聽聽聽聽聽聽//聽XXX聽Need聽to聽have聽a聽no-title聽version聽of聽embedded聽windows.聽聽
  20. 聽聽聽聽聽聽聽聽layoutResource聽=聽com.android.internal.R.layout.screen_progress;聽聽
  21. 聽聽聽聽聽聽聽聽//聽System.out.println("Progress!");聽聽
  22. 聽聽聽聽}聽聽聽
  23. 聽聽聽聽//...聽聽
  24. 聽聽聽聽聽聽
  25. 聽聽聽聽//3聽閫夊畾浜嗙獥鍙d慨楗板竷灞?鏂囦欢聽锛屾坊鍔犺嚦DecorView瀵硅薄閲岋紝骞朵笖鎸囧畾mcontentParent鍊?/span>聽聽
  26. 聽聽聽聽View聽in聽=聽mLayoutInflater.inflate(layoutResource,聽null);聽聽
  27. 聽聽聽聽decor.addView(in,聽new聽ViewGroup.LayoutParams(MATCH_PARENT,聽MATCH_PARENT));聽聽
  28. 聽聽
  29. 聽聽聽聽ViewGroup聽contentParent聽=聽(ViewGroup)findViewById(ID_ANDROID_CONTENT);聽聽
  30. 聽聽聽聽if聽(contentParent聽==聽null)聽{聽聽
  31. 聽聽聽聽聽聽聽聽thrownew聽RuntimeException("Window聽couldn't聽find聽content聽container聽view");聽聽
  32. 聽聽聽聽}聽聽
  33. 聽聽
  34. 聽聽聽聽if聽((features聽&聽(1聽<<聽FEATURE_INDETERMINATE_PROGRESS))聽!=聽0)聽{聽聽
  35. 聽聽聽聽聽聽聽聽ProgressBar聽progress聽=聽getCircularProgressBar(false);聽聽
  36. 聽聽聽聽聽聽聽聽if聽(progress聽!=聽null)聽{聽聽
  37. 聽聽聽聽聽聽聽聽聽聽聽聽progress.setIndeterminate(true);聽聽
  38. 聽聽聽聽聽聽聽聽}聽聽
  39. 聽聽聽聽}聽聽
  40. 聽聽聽聽//...聽聽
  41. 聽聽聽聽return聽contentParent;聽聽
  42. }聽聽



璇ユ柟娉曚細鍋氬?涓嬩簨鎯咃細

聽 聽1銆佹牴鎹?獥鍙g殑椋庢牸淇?グ绫诲瀷涓鸿?绐楀彛閫夋嫨涓嶅悓鐨勭獥鍙e竷灞?鏂囦欢(鏍硅?鍥?銆傝繖浜涚獥鍙d慨楗板竷灞?鏂囦欢鎸囧畾涓?涓?敤鏉ュ瓨鏀?/span>

聽聽聽聽聽聽聽聽聽Activity鑷?畾涔夊竷灞?鏂囦欢鐨刅iewGroup瑙嗗浘锛屼竴鑸?负FrameLayout 鍏秈d 涓猴細 android:id="@android:id/content"銆?/span>

聽聽聽聽聽聽聽聽渚嬪?绐楀彛淇?グ绫诲瀷鍖呮嫭FullScreen(鍏ㄥ睆)銆丯oTitleBar(涓嶅惈鏍囬?鏍?绛夈?傞?夊畾绐楀彛淇?グ绫诲瀷鏈変袱绉嶏細

聽聽聽聽聽聽聽聽聽聽聽鈶犮?佹寚瀹歳equestFeature()鎸囧畾绐楀彛淇?グ绗︼紝PhoneWindow瀵硅薄璋冪敤getLocalFeature()鏂规硶鑾峰彇鍊硷紱

聽聽聽聽聽聽聽聽聽聽聽鈶°?佷负鎴戜滑鐨凙ctivity閰嶇疆鐩稿簲灞炴?э紝鍗砤ndroid锛歵heme=鈥溾?濓紝PhoneWindow瀵硅薄璋冪敤getWindowStyle()鏂规硶

聽聽聽聽聽聽聽聽聽聽聽聽聽 鑾峰彇鍊笺??/span>

聽聽聽聽聽聽聽聽涓句緥濡備笅锛岄殣钘忔爣棰樻爮鏈夊?涓嬫柟娉曪細requestWindowFeature(Window.FEATURE_NO_TITLE);

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 鎴栬??涓篈ctivity閰嶇疆xml灞炴?э細android:theme=鈥滰android:style/Theme.NoTitleBar鈥濄??/span>

聽聽聽聽聽聽聽聽PS锛氬洜姝わ紝鍦ˋctivity涓?繀椤诲湪setContentView涔嬪墠璋冪敤requestFeature()鏂规硶銆?/span>

聽聽纭?畾濂界獥鍙i?鏍间箣鍚庯紝閫夊畾璇ラ?鏍煎?搴旂殑甯冨眬鏂囦欢锛岃繖浜涘竷灞?鏂囦欢浣嶄簬 frameworks/base/core/res/layout/聽聽锛?/span>

聽聽聽聽聽聽聽聽鍏稿瀷鐨勭獥鍙e竷灞?鏂囦欢鏈夛細

聽聽聽聽聽聽聽聽聽聽R.layout.dialog_titile_icons聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽R.layout.screen_title_icons

聽聽聽聽聽聽聽聽聽聽R.layout.screen_progress聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽R.layout.dialog_custom_title

聽聽聽聽聽聽聽聽聽聽R.layout.dialog_title聽聽聽

聽聽聽聽聽聽聽聽聽聽R.layout.screen_title聽聽聽聽聽聽聽聽 // 鏈?甯哥敤鐨凙ctivity绐楀彛淇?グ甯冨眬鏂囦欢

聽聽聽聽聽聽聽聽聽聽R.layout.screen_simple聽聽聽 //鍏ㄥ睆鐨凙ctivity绐楀彛甯冨眬鏂囦欢

聽 聽鍒嗘瀽Activity鏈?甯哥敤鐨勪竴绉嶇獥鍙e竷灞?鏂囦欢锛?span style="color: #660000;">R.layout.screen_title聽 锛?/span>

[java]聽view plaincopyprint?
  1. <!--聽聽
  2. This聽is聽an聽optimized聽layout聽for聽a聽screen,聽with聽the聽minimum聽set聽of聽features聽聽
  3. enabled.聽聽
  4. -->聽聽
  5. 聽聽
  6. <LinearLayout聽xmlns:android="http://schemas.android.com/apk/res/android"聽聽
  7. 聽聽聽聽android:orientation="vertical"聽聽
  8. 聽聽聽聽android:fitsSystemWindows="true">聽聽
  9. 聽聽聽聽<FrameLayout聽聽
  10. 聽聽聽聽聽聽聽聽android:layout_width="match_parent"聽聽聽
  11. 聽聽聽聽聽聽聽聽android:layout_height="?android:attr/windowTitleSize"聽聽
  12. 聽聽聽聽聽聽聽聽style="?android:attr/windowTitleBackgroundStyle">聽聽
  13. 聽聽聽聽聽聽聽聽<TextView聽android:id="@android:id/title"聽聽聽
  14. 聽聽聽聽聽聽聽聽聽聽聽聽style="?android:attr/windowTitleStyle"聽聽
  15. 聽聽聽聽聽聽聽聽聽聽聽聽android:background="@null"聽聽
  16. 聽聽聽聽聽聽聽聽聽聽聽聽android:fadingEdge="horizontal"聽聽
  17. 聽聽聽聽聽聽聽聽聽聽聽聽android:gravity="center_vertical"聽聽
  18. 聽聽聽聽聽聽聽聽聽聽聽聽android:layout_width="match_parent"聽聽
  19. 聽聽聽聽聽聽聽聽聽聽聽聽android:layout_height="match_parent"聽/>聽聽
  20. 聽聽聽聽</FrameLayout>聽聽
  21. 聽聽聽聽<FrameLayout聽android:id="@android:id/content"聽聽
  22. 聽聽聽聽聽聽聽聽android:layout_width="match_parent"聽聽聽
  23. 聽聽聽聽聽聽聽聽android:layout_height="0dip"聽聽
  24. 聽聽聽聽聽聽聽聽android:layout_weight="1"聽聽
  25. 聽聽聽聽聽聽聽聽android:foregroundGravity="fill_horizontal|top"聽聽
  26. 聽聽聽聽聽聽聽聽android:foreground="?android:attr/windowContentOverlay"聽/>聽聽
  27. </LinearLayout>聽聽

聽 聽 聽 聽璇ュ竷灞?鏂囦欢寰堢畝鍗曪紝涓?涓狶inearLayout涓嬪寘鍚?簡涓や釜瀛怓rameLayout瑙嗗浘锛岀?涓?涓狥rameLayout鐢ㄦ潵鏄剧ず鏍囬?鏍?TitleBar)锛?/span>

聽 璇?extView 瑙嗗浘id涓簍itle(android:id="@android:id/title")锛涚?浜屼釜FrameLayout鐢ㄦ潵鏄剧ず鎴戜滑Activity鐨勫竷灞?鏂囦欢鐨勭埗瑙嗗浘锛?/span>

聽 璇?rameLayoutid涓篶ontent(android:id="@android:id/content")聽銆?/span>

聽聽鍏ㄥ睆鐨勭獥鍙e竷灞?鏂囦欢R.layout.screen_simple:

[java]聽view plaincopyprint?
  1. <--This聽is聽an聽optimized聽layout聽for聽a聽screen,聽with聽the聽minimum聽set聽of聽features聽聽
  2. enabled.聽聽
  3. -->聽聽
  4. 聽聽
  5. <FrameLayout聽xmlns:android="http://schemas.android.com/apk/res/android"聽聽
  6. 聽聽聽聽android:id="@android:id/content"聽聽
  7. 聽聽聽聽android:fitsSystemWindows="true"聽聽
  8. 聽聽聽聽android:foregroundInsidePadding="false"聽聽
  9. 聽聽聽聽android:foregroundGravity="fill_horizontal|top"聽聽
  10. 聽聽聽聽android:foreground="?android:attr/windowContentOverlay"聽/>聽聽

聽 聽 聽

聽 聽 聽 聽 聽 聽聽璇ュ竷灞?鏂囦欢鍙?湁涓?涓狥rameLayout锛岀敤鏉ユ樉绀烘垜浠珹ctivity鐨勫竷灞?鏂囦欢锛岃?FrameLayoutid涓?/span>

聽 聽 android:id="@android:id/content"

聽 2銆佸墠闈?竴姝ユ垜浠?‘瀹氱獥鍙d慨楗板竷灞?鏂囦欢鍚庯紝mDecor鍋氫负鏍硅?鍥惧皢璇ョ獥鍙e竷灞?瀵瑰簲鐨勮?鍥炬坊鍔犺繘鍘伙紝骞朵笖鑾峰彇id涓篶ontent

聽聽聽聽聽聽聽聽聽聽鐨刅iew锛屽皢鍏惰祴鍊肩粰mContentParent瀵硅薄锛屽嵆鎴戜滑鍓嶉潰涓?彁鍒扮殑绗?簩涓狥rameLayout銆?/span>

聽聽At Last銆佷骇鐢熶簡mDecor鍜宮ContentParent瀵硅薄鍚庯紝灏卞皢鎴戜滑鐨凙ctivity甯冨眬鏂囦欢鐩存帴娣诲姞鑷砿ContentParent鐖惰?鍥句腑鍗冲彲銆?/span>

聽 聽 聽 鎴戜滑鍐嶆?鍥炲埌 Step 2 涓璓honeWindow.setContentView()聽聽聽聽聽 璇ユ柟娉曞湪PhoneWindow绫讳腑

[java]聽view plaincopyprint?
  1. @Override聽聽
  2. publicvoid聽setContentView(int聽layoutResID)聽{聽聽
  3. 聽聽聽聽if聽(mContentParent聽==聽null)聽{聽聽
  4. 聽聽聽聽聽聽聽聽installDecor();聽聽
  5. 聽聽聽聽}聽else聽{聽聽
  6. 聽聽聽聽聽聽聽聽mContentParent.removeAllViews();聽聽
  7. 聽聽聽聽}聽聽
  8. 聽聽聽聽mLayoutInflater.inflate(layoutResID,聽mContentParent);聽聽
  9. 聽聽聽聽final聽Callback聽cb聽=聽getCallback();聽聽
  10. 聽聽聽聽if聽(cb聽!=聽null)聽{聽聽
  11. 聽聽聽聽聽聽聽聽cb.onContentChanged();聽聽
  12. 聽聽聽聽}聽聽
  13. }聽聽



聽聽鏁翠釜杩囩▼涓昏?鏄??浣曟妸Activity鐨勫竷灞?鏂囦欢娣诲姞鑷崇獥鍙i噷锛屼笂闈㈢殑杩囩▼鍙?互姒傛嫭涓猴細

聽聽聽聽聽聽聽聽聽聽聽聽聽 1銆佸垱寤轰竴涓狣ecorView瀵硅薄锛岃?瀵硅薄灏嗕綔涓烘暣涓?簲鐢ㄧ獥鍙g殑鏍硅?鍥?/span>

聽聽聽聽聽聽聽聽聽聽聽聽聽 2銆佸垱寤轰笉鍚岀殑绐楀彛淇?グ甯冨眬鏂囦欢锛屽苟涓旇幏鍙朅ctivity鐨勫竷灞?鏂囦欢璇ュ瓨鏀剧殑鍦版柟锛岀敱璇ョ獥鍙d慨楗板竷灞?鏂囦欢鍐卛d涓篶ontent鐨?/span>

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 FrameLayout鎸囧畾 銆?/span>

聽聽聽聽聽聽聽聽聽聽聽聽聽 3銆佸皢Activity鐨勫竷灞?鏂囦欢娣诲姞鑷砳d涓篶ontent鐨凢rameLayout鍐呫??/span>

聽 聽 聽 聽鏈?鍚庯紝褰揂MS(ActivityManagerService)鍑嗗?resume涓?涓狝ctivity鏃讹紝浼氬洖璋冭?Activity鐨刪andleResumeActivity()鏂规硶锛?/span>

聽 璇ユ柟娉曚細璋冪敤Activity鐨刴akeVisible鏂规硶 ,鏄剧ず鎴戜滑鍒氭墠鍒涘缓鐨刴Decor 瑙嗗浘鏃忋??/span>

聽 聽

[java]聽view plaincopyprint?
  1. //绯荤粺resume涓?涓狝ctivity鏃讹紝璋冪敤姝ゆ柟娉?/span>聽聽
  2. finalvoid聽handleResumeActivity(IBinder聽token,聽boolean聽clearHide,聽boolean聽isForward)聽{聽聽
  3. 聽聽聽聽ActivityRecord聽r聽=聽performResumeActivity(token,聽clearHide);聽聽
  4. 聽聽聽聽//...聽聽
  5. 聽聽聽聽聽if聽(r.activity.mVisibleFromClient)聽{聽聽
  6. 聽聽聽聽聽聽聽聽聽r.activity.makeVisible();聽聽
  7. 聽聽聽聽聽}聽聽
  8. }聽聽

聽 聽 handleResumeActivity()鏂规硶鍘熷瀷濡備笅锛?浣嶄簬ActivityThread绫讳腑

[java]聽view plaincopyprint?
  1. void聽makeVisible()聽{聽聽
  2. 聽聽聽聽if聽(!mWindowAdded)聽{聽聽
  3. 聽聽聽聽聽聽聽聽ViewManager聽wm聽=聽getWindowManager();聽聽聽//聽鑾峰彇WindowManager瀵硅薄聽聽
  4. 聽聽聽聽聽聽聽聽wm.addView(mDecor,聽getWindow().getAttributes());聽聽
  5. 聽聽聽聽聽聽聽聽mWindowAdded聽=聽true;聽聽
  6. 聽聽聽聽}聽聽
  7. 聽聽聽聽mDecor.setVisibility(View.VISIBLE);聽//浣垮叾澶勪簬鏄剧ず鐘跺喌聽聽
  8. }聽聽

聽 聽 聽鎺ヤ笅鏉ュ氨鏄?紝濡備綍鎶婃垜浠?凡缁忓垱寤哄ソ鐨勭獥鍙i?氱煡缁橶indowManagerService 锛屼互渚垮畠鑳藉?鎶婅繖涓?獥鍙f樉绀哄湪灞忓箷涓娿??/span>

鍏充簬杩欐柟闈㈠唴瀹瑰ぇ瀹跺彲浠ュ幓鐪嬮倱鍑″钩鑰佸笀鐨勮繖绡囧崥瀹???a href="http://www.cnblogs.com/innost/archive/2011/02/05/1949240.html" target="_blank" style="color: #ca0000;">Android娣卞叆娴呭嚭涔婼urface[1]聽銆?/span>

[java]聽view plaincopyprint?
  1. <pre></pre>聽聽
  2. <pre></pre>聽聽
  3. <pre></pre>聽聽
  4. <pre></pre>聽聽
  5. <pre></pre>聽聽
  6. <pre></pre>聽聽
  7. <pre></pre>聽聽
  8. <pre></pre>聽聽
  9. <pre></pre>聽聽
  10. <pre></pre>聽聽
  11. <pre></pre>聽聽
  12. <pre></pre>聽聽
  13. <pre></pre>聽聽
  14. <pre></pre> 聽
  相关解决方案