How to apply a color filter to a view with all children

前端 未结 5 565
误落风尘
误落风尘 2020-12-24 02:45

How do I grey out a view uniformly which contains many different items - ImageViews, TextViews, background image. Do I have to grey out each thing individually? Or is there

5条回答
  •  轮回少年
    2020-12-24 03:03

    It depends entirely on the method you are using to "gray out" items. If you are doing so by calling setEnabled(false) on the parent ViewGroup, by default state flags (like disabled) do not trickle down to the child views. However, there are two simple ways you can customize this:

    One option is to add the attribute android:duplicateParentState="true" to each child view in your XML. This will tell the children to get their state flags from the parent. This will mirror ALL flags however, including pressed, checked, etc...not just enabled.

    Another option is to create a custom subclass of your ViewGroup and override setEnabled() to call all the child views as well, i.e.

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
    
        for(int i=0; i < getChildCount(); i++) {
            getChildAt(i).setEnabled(enabled);
        }
    }
    

提交回复
热议问题