Android layout 获取活动';从片段中删除布局并阻止其所有子项
我在多个活动中使用一个片段来显示ShowCaseView,一切正常,但我想禁用ShowCaseView在演示过程中概述的控件,防止在演示结束前单击它们 就像这样:Android layout 获取活动';从片段中删除布局并阻止其所有子项,android-layout,android-fragments,Android Layout,Android Fragments,我在多个活动中使用一个片段来显示ShowCaseView,一切正常,但我想禁用ShowCaseView在演示过程中概述的控件,防止在演示结束前单击它们 就像这样: getActivity().getWindow().getDecorView().findViewById(android.R.id.content).findViewById(R.id.btnJournal).setEnabled(false); 我可以禁用一个我知道的元素,但我想在不显式写入id的情况下禁用该视图的所有子元素,但
getActivity().getWindow().getDecorView().findViewById(android.R.id.content).findViewById(R.id.btnJournal).setEnabled(false);
我可以禁用一个我知道的元素,但我想在不显式写入id的情况下禁用该视图的所有子元素,但我通过getActivity().getWindow().getDecorView().findViewById(android.R.id.content)
得到的是一个视图,而不是ViewGroup或LinearLayout,如果我将其投射到ViewGroup-它找不到任何子对象,如果我将其投射到LinearLayout,那么,好:
`Caused by: java.lang.ClassCastException: com.android.internal.policy.impl.PhoneWindow$DecorView cannot be cast to android.widget.LinearLayout`
如何遍历它的子项?我找到了下一个解决方案: 首先,我使用了来自的代码片段-感谢MH。要获取所有子视图,请执行以下操作:
private List<View> getAllChildrenBFS(View v) {
List<View> visited = new ArrayList<View>();
List<View> unvisited = new ArrayList<View>();
unvisited.add(v);
while (!unvisited.isEmpty()) {
View child = unvisited.remove(0);
visited.add(child);
if (!(child instanceof ViewGroup)) continue;
ViewGroup group = (ViewGroup) child;
final int childCount = group.getChildCount();
for (int i=0; i<childCount; i++) unvisited.add(group.getChildAt(i));
}
return visited;
}
然后是一个类似的代码,用于在作业完成后将其设置为启用:
ViewGroup mRootView = (ViewGroup) getActivity().getWindow().getDecorView().findViewById(android.R.id.content);
for (View v : getAllChildrenBFS(mRootView)) v.setEnabled(true);
所以现在我很冷静,很高兴我没有白白浪费一整天
ViewGroup mRootView = (ViewGroup) getActivity().getWindow().getDecorView().findViewById(android.R.id.content);
for (View v : getAllChildrenBFS(mRootView)) v.setEnabled(true);