android - 如何在 subview 组上设置属性android

标签 android attributes set

假设您的 linerlayout 有一堆子元素,例如 TextView 和复选框以及...。如何在 linerlayout 中的所有子项上设置 setAlpha 或 setClickable 或 ... 等属性。我可以通过使用 findViewById 找到每个 child 然后设置属性来做到这一点,但我在 linerlayout 中有很多 child

最佳答案

递归迭代所有子项,检查 View 是否是 ViewGroup 的实例(即包含其他子项):

public void setAlpha(View view, float alpha){
    if (view instanceof ViewGroup){
        ViewGroup viewgroup = (ViewGroup)view;
        int count = viewgroup.getChildCount();
        for (int i=0; i < count; i++){
            View viewNext = viewgroup.getChildAt(i);
            setAlpha(viewNext, alpha);
        }
    }
    else {
        view.setAlpha(alpha);
    }
}

并将其应用于您的布局:

setAlpha(findViewById(R.id.your_layout), alpha);

关于android - 如何在 subview 组上设置属性android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23083165/

相关文章:

java - Grails:检查分离的对象是否在附加的集合中

带有音频 Sprite 的 Android HTML5 音频元素

java - 安卓/PhoneGap : Using third-party libraries in plugin-development

c# - 如何国际化表示 C# 枚举值的字符串?

c# - 如何在运行时从 NUnit 测试运行中获取单元测试方法属性?

c++11,抢救一个集合

python - 设置为 dict Python

android - 响应 304 : Not Modified has no content

android - 如何使用 DownloadManager 列

javascript - Jquery : how to add attributs dynamically with the function . 属性()