首页 > 编程知识 正文

setparams,figtree使用说明

时间:2023-05-05 16:02:18 阅读:44053 作者:2995

我们使用活动。 设置布局时使用setcontentview (r.layout.activity _ home )。 那到底是怎么加载的呢?

publicclasshomeactivityextendsappcompatactivity { @ overrideprotectedvoidoncreate } { supplablebundlesavedinstancestate } setate }1首先进入appcompatactivity---- setcontentview, 浏览源代码publicclassappcompatactivityextendsfragmentactivityimplementsappcompatcality taskstackbuilder.supportparentable, actionbardrawertoggle.delegate provider//首先调用的是父类(AppCompatActivity (的setcontentView方法@ overridepublicvoidsity ) 随后调用的AppCompatDelegate方法(//appcompatdelegate类表示请求, 在任何活动中支持app compat @ nonnullpublicappcompatdelegategetdelegate () if(mdelegate==null ) mdelegate=appcompate ) }} 2 AppCompatDelegate是抽象类,AppCompatDelegateImpl是抽象类的实例。 activity的步骤都是在这种情况下代理操作。

publicabstractclassappcompatdelegate { @ nonnullpublicstaticappcompatdelegatecreate (@ nonnullablactivityactivity,@ nullableableapc ) //在实际实例中操作}} 3AppCompatDelegateImpl的具体步骤

@restrictto(library ) classappcompatdelegateimplextendsappcompatdelegateimplementsmenubuilder.callback, layoutinflater.calllback当前活动和当前活动窗口. callbackappcompatdelegateimpl (活动活动,应用程序兼容性缓存}2//privateappcompatdelegateimpl (上下文、窗口、appcompatcallback、对象主机) { m context=ccontext m主机=主机; //当前活动if (mlocalnightmode==mode _ night _ unspecifiedmhostinstanceofdialog (finalappcompatactivityactivity=tty )=null )//thiscodepathisusedtodetectwhenthisdelegateisachilddelegatefrom//an activity,primarilyfordialogs.dialogsususus

want to make sure that the this 'child' delegate does not interfere // with the Activity config. The simplest way to do that is to match the // outer Activity's local night mode mLocalNightMode = activity.getDelegate().getLocalNightMode(); } } if (mLocalNightMode == MODE_NIGHT_UNSPECIFIED) { // Try and read the current night mode from our static store final Integer value = sLocalNightModes.get(mHost.getClass()); if (value != null) { mLocalNightMode = value; // Finally remove the value sLocalNightModes.remove(mHost.getClass()); } } if (window != null) { attachToWindow(window); } // Preload appcompat-specific handling of drawables that should be handled in a special // way (for tinting etc). After the following line completes, calls from AppCompatResources // to ResourceManagerInternal (in appcompat-resources) will handle those internal drawable // paths correctly without having to go through AppCompatDrawableManager APIs. AppCompatDrawableManager.preload(); }3//读取样式,设置window 背景等private void attachToWindow(@NonNull Window window) { if (mWindow != null) { throw new IllegalStateException( "AppCompat has already installed itself into the Window"); } final Window.Callback callback = window.getCallback(); if (callback instanceof AppCompatWindowCallback) { throw new IllegalStateException( "AppCompat has already installed itself into the Window"); } mAppCompatWindowCallback = new AppCompatWindowCallback(callback); // Now install the new callback window.setCallback(mAppCompatWindowCallback); final TintTypedArray a = TintTypedArray.obtainStyledAttributes( mContext, null, sWindowBackgroundStyleable); final Drawable winBg = a.getDrawableIfKnown(0); if (winBg != null) { // Now set the background drawable window.setBackgroundDrawable(winBg); } a.recycle(); mWindow = window; }4 //调用setContentView @Override public void setContentView(int resId) { ensureSubDecor(); //确保 phonewindow decordeview 绘制完成 ViewGroup contentParent = mSubDecor.findViewById(android.R.id.content); //content 是我们自己绘制view 的父布局 contentParent.removeAllViews(); //先清理view LayoutInflater.from(mContext).inflate(resId, contentParent);//通过LayoutInflater 解析xml 文件生成view,然后contentParent.addview(读取resId 创建的view); mAppCompatWindowCallback.getWrapped().onContentChanged(); }4.1//确保创建 主题样式的根布局 private void ensureSubDecor() { if (!mSubDecorInstalled) { mSubDecor = createSubDecor(); // If a title was set before we installed the decor, propagate it now CharSequence title = getTitle(); if (!TextUtils.isEmpty(title)) { if (mDecorContentParent != null) { mDecorContentParent.setWindowTitle(title); } else if (peekSupportActionBar() != null) { peekSupportActionBar().setWindowTitle(title); } else if (mTitleView != null) { mTitleView.setText(title); } } applyFixedSizeWindow(); onSubDecorInstalled(mSubDecor); mSubDecorInstalled = true; // Invalidate if the panel menu hasn't been created before this. // Panel menu invalidation is deferred avoiding application onCreateOptionsMenu // being called in the middle of onCreate or similar. // A pending invalidation will typically be resolved before the posted message // would run normally in order to satisfy instance state restoration. PanelFeatureState st = getPanelState(FEATURE_OPTIONS_PANEL, false); if (!mIsDestroyed && (st == null || st.menu == null)) { invalidatePanelMenu(FEATURE_SUPPORT_ACTION_BAR); } } }

总结:调用流程:我们创建的activity.setcontentview() --> AppCompatActivity.setcontentview() -->AppCompatDelegateImpl.setcontentview()

如何把xml 文件添加到根视图呢?通过 LayoutInflater.inflate(),在通过XmlResourceParser 解析xml 文件,生成xmlview,然后 contentv view.add(xmlview),一个view 树就创建完了。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。