Android:如何将样式应用于 ViewGroup 的所有子级

标签 android android-styles

我有一个已应用于 View 组的样式,但我想将该样式应用于 View 组的所有子项。怎么办?

最佳答案

除此之外没有其他方法可以做到这一点:
您可以通过在 LinearLayout 上使用 next ViewIterator 进行迭代,并通过 instanceof 检查 EditText,然后更改您需要的任何属性。

    public class ViewIterator implements java.util.Iterator<View>
{
    private java.util.Stack<View> mNodes;

    public ViewIterator(View view)
    {
        mNodes = new java.util.Stack<View>();
        if (view != null)
        {
            mNodes.push(view);
        }
    }

    public boolean hasNext()
    {
        return !mNodes.empty();
    }

    public View next()
    {
        View view = null;

        // if remaining nodes
        if (hasNext())
        {
            // return the last element
            view = mNodes.pop();
            assert (view != null);
            // if it is a container, add its children to the stack
            if (view instanceof ViewGroup || view instanceof AdapterView)
            {
                ViewGroup viewGroup = (ViewGroup)view;
                for (int i = 0; i < viewGroup.getChildCount(); i++)
                {
                    assert (viewGroup.getChildAt(i) != null);
                    mNodes.push(viewGroup.getChildAt(i));
                }
            }
        }

        // return result
        return view;
    }

    public void remove()
    {
        // not supported
    }
}

取自 here .

关于Android:如何将样式应用于 ViewGroup 的所有子级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25481378/

相关文章:

android - Moodle - 试图获得 token ,收到 Forbidden 403

Java - Android - MyCustomAdapter

android - 在对话框中更改 TextView 等的主题?

android - 如何减小 FastScroll 拇指大小

android - 从使用 Widget.EditText 样式的 TextView 中删除下划线

android - 全屏自定义主题

android - 如何像这样在android中创建圆角标签?

android - 特定三星设备上的指纹崩溃

android - 如何从屏幕底部获取弹出菜单?

android - 没有找到类 "com.google.android.material.button.MaterialButton"