你的位置:首页 > 软件开发 > 操作系统 > Android GUI之Activity、Window、View

Android GUI之Activity、Window、View

发布时间:2015-07-23 14:00:15
相信大家在接触Android之初就已经知道了Activity中的setContentView方法的作用了,很明显此方法是用于为Activity填充相应的布局的。那么,Activity是如何将填充的布局绘制出来的呢?实际上Activity将View的绘制与显示交给了Window对象 ...

Android GUI之Activity、Window、View

  相信大家在接触Android之初就已经知道了Activity中的setContentView方法的作用了,很明显此方法是用于为Activity填充相应的布局的。那么,Activity是如何将填充的布局绘制出来的呢?实际上Activity将View的绘制与显示交给了Window对象来处理,下面我们通过源码来进行跟踪分析。

  Activity的源码如下,只给出我们关注的部分:

public class Activity extends ContextThemeWrapper    implements LayoutInflater.Factory2,    Window.Callback, KeyEvent.Callback,    OnCreateContextMenuListener, ComponentCallbacks2,    Window.OnWindowDismissedCallback {   ……  ……  private Window mWindow;  private WindowManager mWindowManager;  …… /**   * Retrieve the current {@link android.view.Window} for the activity.   * This can be used to directly access parts of the Window API that   * are not available through Activity/Screen.   *   * @return Window The current window, or null if the activity is not   *     visual.   */  public Window getWindow() {    return mWindow;  }  ……  /**   * Set the activity content from a layout resource. The resource will be   * inflated, adding all top-level views to the activity.   *   * @param layoutResID Resource ID to be inflated.   *   * @see #setContentView(android.view.View)   * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)   */  public void setContentView(int layoutResID) {    getWindow().setContentView(layoutResID);    initWindowDecorActionBar();  }  /**   * Set the activity content to an explicit view. This view is placed   * directly into the activity's view hierarchy. It can itself be a complex   * view hierarchy. When calling this method, the layout parameters of the   * specified view are ignored. Both the width and the height of the view are   * set by default to {@link ViewGroup.LayoutParams#MATCH_PARENT}. To use   * your own layout parameters, invoke   * {@link #setContentView(android.view.View,android.view.ViewGroup.LayoutParams)}   * instead.   *   * @param view The desired content to display.   *   * @see #setContentView(int)   * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)   */  public void setContentView(View view) {    getWindow().setContentView(view);    initWindowDecorActionBar();  }  final void attach(Context context, ActivityThread aThread,      Instrumentation instr, IBinder token, int ident,      Application application, Intent intent, ActivityInfo info,      CharSequence title, Activity parent, String id,      NonConfigurationInstances lastNonConfigurationInstances,      Configuration config, IVoiceInteractor voiceInteractor) {    attachBaseContext(context);    mFragments.attachActivity(this, mContainer, null);    mWindow = PolicyManager.makeNewWindow(this);       ……  }……}


原标题:Android GUI之Activity、Window、View

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录