我有一个已应用于 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/