草庐IT

android - 何时将 subview 从 XML 添加到 Layout/ViewGroup

coder 2023-11-19 原文

我的问题是:
我想知道 xLayout(或一般的 ViewGroup)何时从 XML 添加 subview ?我所说的“何时”是指在代码的哪个点,在 UI 工具包的“遍历”的哪个“ channel ”中?
我应该覆盖 xLayout 或 ViewGroup 的哪个方法?

我做了功课:我看过"Writing Custom Views For Android"在上次 Google I/O 中提出(由 Adam Powell 和 Romain Guy),我已经阅读了 Adam Powell 对此 Google+ post 的评论.

最佳答案

Looking for the exact point in Android's source code where children are added.



大家可以看看setContentView(R.layout.some_id)正在做幕后工作。
setContentView(int)来电PhoneWindow#setContentView(int) - PhoneWindow LinkWindow 的具体实现:
@Override
public void setContentView(int layoutResID) {
    if (mContentParent == null) {
        installDecor();
    } else {
        mContentParent.removeAllViews();
    }
    mLayoutInflater.inflate(layoutResID, mContentParent);
    final Callback cb = getCallback();
    if (cb != null && !isDestroyed()) {
        cb.onContentChanged();
    }
}

方法LayoutInflater#inflate(layoutResID, mContentParent)最终调用 ViewGroup#addView(View, LayoutParams)mContentParent .介于两者之间, subview

I want to know what happens exactly after I set content view to an XML file that contains a custom view. Afer the constructor there has to be a part in the code where the custom view "parse/read/inflate/convert" XML-declared child views to actual views ! (comment by JohnTube)



模糊性:从 JohnTube 的评论来看,他似乎更感兴趣的是了解自定义 View 是如何膨胀的。要知道这一点,我们必须看看 LayoutInflater 的工作原理。 Link .

所以,Which method of xLayout or ViewGroup should I override ? 的答案是 ViewGroup#addView(View, LayoutParams) .请注意,此时,所有常规/自定义 View 的膨胀已经发生。

自定义 View 的膨胀:
LayoutInflater中的以下方法是addView(View, LayoutParams)在父/根上调用:

注:调用mLayoutInflater.inflate(layoutResID, mContentParent);PhoneWindow#setContentView(int)链到这个。这里mContentParentDecorView : 可通过 getWindow().getDecorView() 访问的 View .
// Inflate a new view hierarchy from the specified XML node.
public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)

// Recursive method used to descend down the xml hierarchy and instantiate views,     
// instantiate their children, and then call onFinishInflate().
void rInflate(XmlPullParser parser, View parent, final AttributeSet attrs,
       boolean finishInflate) throws XmlPullParserException, IOException

此方法(以及递归 rInflate(XmlPullParser, View, AttributeSet, boolean) )中感兴趣的调用是:
temp = createViewFromTag(root, name, attrs);

让我们看看createViewFromTag(...)是在做:
View createViewFromTag(View parent, String name, AttributeSet attrs) {
    ....
    ....
    if (view == null) {
        if (-1 == name.indexOf('.')) {
            view = onCreateView(parent, name, attrs);
        } else {
            view = createView(name, null, attrs);
        }
    }
    ....
}

period(.)决定是否 onCreateView(...)createView(...)叫做。

为什么要做这个检查?因为一个 View定义于 android.view , android.widgetandroid.webkit包通过其类名访问。例如:
android.widget: Button, TextView etc.

android.view: ViewStub. SurfaceView, TextureView etc.

android.webkit: WebView

当遇到这些 View 时,onCreateView(parent, name, attrs)叫做。此方法实际上链接到 createView(...) :
protected View onCreateView(String name, AttributeSet attrs) throws ClassNotFoundException {
    return createView(name, "android.view.", attrs);
}

这将处理 SurfaceView , TextureView以及在 android.view 中定义的其他 View 包裹。如果您有兴趣了解 TextView, Button etc.正在处理,看PhoneLayoutInflater Link - 它扩展了 LayoutInflater并覆盖 onCreateView(...)检查是否 android.widgetandroid.webkit是预期的包名称。事实上,调用getLayoutInflater()为您提供 PhoneLayoutInflater 的实例.这就是为什么如果你要继承 LayoutInflater , 你甚至不能为最简单的布局充气 - 因为 LayoutInflater只能处理来自 android.view 的 View 包裹。

无论如何,我离题了。这个额外的位发生在常规 View 中——没有 period(.)在他们的定义中。自定义 View 的名称中有句点 - com.my.package.CustomView .这就是 LayoutInflater区分两者。

因此,在常规 View (例如 Button)的情况下,prefixandroid.widget将作为第二个参数传递 - 对于自定义 View ,这将是 null . prefix然后与 name 一起使用获取该特定 View 类的构造函数。自定义 View 不需要这个,因为它们的 name已经完全合格了。我想这样做是为了方便。否则,您会以这种方式定义布局:
<android.widget.LinearLayout
    ...
    ... />  

(虽然它是合法的......)

此外,这也是来自支持库(例如 )的 View 必须使用完全限定名称的原因。

顺便说一句,如果您确实想将布局编写为:
<MyCustomView ../>

你所要做的就是扩展 LayoutInflater 并添加你的包名 com.my.package.到膨胀期间检查的字符串列表。查看 PhoneLayoutInflater寻求帮助。

让我们看看自定义 View 和常规 View 在最后阶段会发生什么 - createView(...) :
public final View createView(String name, String prefix, AttributeSet attrs)
                            throws ClassNotFoundException, InflateException {

    // Try looking for the constructor in cache
    Constructor<? extends View> constructor = sConstructorMap.get(name);
    Class<? extends View> clazz = null;

    try {
        if (constructor == null) {
            // Class not found in the cache, see if it's real, and try to add it
            clazz = mContext.getClassLoader().loadClass(
                 prefix != null ? (prefix + name) : name).asSubclass(View.class);
            ....
            // Get constructor   
            constructor = clazz.getConstructor(mConstructorSignature);
            sConstructorMap.put(name, constructor);
        } else {
            ....
        }

        Object[] args = mConstructorArgs;
        args[1] = attrs;

        // Obtain an instance
        final View view = constructor.newInstance(args);
        ....

        // We finally have a view!
        return view;
    }
    // A bunch of catch blocks: 
        - if the only constructor defined is `CustomView(Context)` - NoSuchMethodException
        - if `com.my.package.CustomView` doesn't extend View - ClassCastException
        - if `com.my.package.CustomView` is not found - ClassNotFoundException

    // All these catch blocks throw the often seen `InflateException`.
}

... 一个 View出生。

关于android - 何时将 subview 从 XML 添加到 Layout/ViewGroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17334078/

有关android - 何时将 subview 从 XML 添加到 Layout/ViewGroup的更多相关文章

  1. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  2. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  3. ruby - 将 Bootstrap Less 添加到 Sinatra - 2

    我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它

  4. ruby - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

  5. ruby - 可以通过多少种方法将方法添加到 ruby​​ 对象? - 2

    当谈到运行时自省(introspection)和动态代码生成时,我认为ruby​​没有任何竞争对手,可能除了一些lisp方言。前几天,我正在做一些代码练习来探索ruby​​的动态功能,我开始想知道如何向现有对象添加方法。以下是我能想到的3种方法:obj=Object.new#addamethoddirectlydefobj.new_method...end#addamethodindirectlywiththesingletonclassclass这只是冰山一角,因为我还没有探索instance_eval、module_eval和define_method的各种组合。是否有在线/离线资

  6. ruby - 如何在 Ruby 中向现有方法定义添加语句 - 2

    我注意到类定义,如果我打开classMyClass,并在不覆盖的情况下添加一些东西我仍然得到了之前定义的原始方法。添加的新语句扩充了现有语句。但是对于方法定义,我仍然想要与类定义相同的行为,但是当我打开defmy_method时似乎,def中的现有语句和end被覆盖了,我需要重写一遍。那么有什么方法可以使方法定义的行为与定义相同,类似于super,但不一定是子类? 最佳答案 我想您正在寻找alias_method:classAalias_method:old_func,:funcdeffuncold_func#similartoca

  7. ruby-on-rails - 添加回形针新样式不影响旧上传的图像 - 2

    我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司

  8. ruby - 我如何添加二进制数据来遏制 POST - 2

    我正在尝试使用Curbgem执行以下POST以解析云curl-XPOST\-H"X-Parse-Application-Id:PARSE_APP_ID"\-H"X-Parse-REST-API-Key:PARSE_API_KEY"\-H"Content-Type:image/jpeg"\--data-binary'@myPicture.jpg'\https://api.parse.com/1/files/pic.jpg用这个:curl=Curl::Easy.new("https://api.parse.com/1/files/lion.jpg")curl.multipart_form_

  9. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  10. ruby-on-rails - 在 Ruby on Rails 中添加 boolean 列值 - 2

    我正在开发一个创建网络博客的RubyonRails项目。我希望将一个名为featured的boolean数据库字段添加到Post模型中。该字段应该可以通过我添加的事件管理界面进行编辑。我使用了以下代码,但我什至没有在网站上显示另一列。$railsgeneratemigrationaddFeaturedfeatured:boolean$rakedb:migrate我是RubyonRails的新手,非常感谢任何帮助。我的index.html.erb文件中的相关代码(views):FeaturedPost架构.rb:ActiveRecord::Schema.define(:version=>

随机推荐