【问题标题】:view group instance查看组实例
【发布时间】:2011-03-11 09:12:12
【问题描述】:

我对 android 很陌生...谁能告诉我如何处理视图组?

例如:

我的main.xml 文件中有一个线性布局。 我能够添加视图的唯一方法是 使用findViewById 并指定线性布局的id。我想开发一种通用方法,它可以处理视图组并执行getChildCount() 之类的功能 等等……

【问题讨论】:

    标签: android viewgroup


    【解决方案1】:

    您可以创建自己的布局子类并覆盖一些方法,例如onFinishInflate()。 View documentation 提供了一些相关信息,您会发现很多关于如何操作的教程。

    这就是我在one of my tutorials 中解释的以获得布局的所有 Checkable 视图。

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    
        final int childCount = this.getChildCount();
        for (int i = 0; i < childCount; ++i) {
            findCheckableChildren(this.getChildAt(i));
        }
    }
    
    /**
     * Add to our checkable list all the children of the view that implement the
     * interface Checkable
     */
    private void findCheckableChildren(View v) {
        if (v instanceof Checkable) {
            this.checkableViews.add((Checkable) v);
        }
    
        if (v instanceof ViewGroup) {
            final ViewGroup vg = (ViewGroup) v;
            final int childCount = vg.getChildCount();
            for (int i = 0; i < childCount; ++i) {
                findCheckableChildren(vg.getChildAt(i));
            }
        }
    }
    

    【讨论】:

    • 谢谢。但我正在编写一个通用类,它获取所有 EditText 字段并将其发布到一个宁静的资源。所以我需要开发一个通用代码,我可以在其他应用程序中使用。因此我不想使用 findViewById 方法,在这种情况下是特定于该应用程序的..
    猜你喜欢
    • 1970-01-01
    • 2018-01-03
    • 1970-01-01
    • 2017-06-22
    • 1970-01-01
    • 2014-02-28
    • 2018-08-13
    • 2014-01-26
    • 1970-01-01
    相关资源
    最近更新 更多