下面列出了android.view.ViewGroup#getChildCount ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Tests scrollability within child views of v given a delta of dx.
*
* @param v View to test for horizontal scrollability
* @param checkV Whether the view v passed should itself be checked for scrollability (true),
* or just its children (false).
* @param dx Delta scrolled in pixels
* @param x X coordinate of the active touch point
* @param y Y coordinate of the active touch point
* @return true if child views of v can be scrolled by delta of dx.
*/
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
if (v instanceof ViewGroup) {
final ViewGroup group = (ViewGroup) v;
final int scrollX = v.getScrollX();
final int scrollY = v.getScrollY();
final int count = group.getChildCount();
// Count backwards - let topmost views consume scroll distance first.
for (int i = count - 1; i >= 0; i--) {
// TODO: Add versioned support here for transformed views.
// This will not work for transformed views in Honeycomb+
final View child = group.getChildAt(i);
if (x + scrollX >= child.getLeft() && x + scrollX < child.getRight() &&
y + scrollY >= child.getTop() && y + scrollY < child.getBottom() &&
canScroll(child, true, dx, x + scrollX - child.getLeft(),
y + scrollY - child.getTop())) {
return true;
}
}
}
return checkV && ViewCompat.canScrollHorizontally(v, -dx);
}
/**
* 为DrawerLayout 布局设置状态栏变色(5.0以下无半透明效果,不建议使用)
*
* @param activity 需要设置的activity
* @param drawerLayout DrawerLayout
* @param color 状态栏颜色值
*/
@Deprecated
public static void setColorForDrawerLayoutDiff(Activity activity, DrawerLayout drawerLayout, @ColorInt int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// 生成一个状态栏大小的矩形
ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0);
if (contentLayout.getChildCount() > 0 && contentLayout.getChildAt(0) instanceof StatusBarView) {
contentLayout.getChildAt(0).setBackgroundColor(calculateStatusColor(color, DEFAULT_STATUS_BAR_ALPHA));
} else {
// 添加 statusBarView 到布局中
StatusBarView statusBarView = createStatusBarView(activity, color);
contentLayout.addView(statusBarView, 0);
}
// 内容布局不是 LinearLayout 时,设置padding top
if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null) {
contentLayout.getChildAt(1).setPadding(0, getStatusBarHeight(activity), 0, 0);
}
// 设置属性
ViewGroup drawer = (ViewGroup) drawerLayout.getChildAt(1);
drawerLayout.setFitsSystemWindows(false);
contentLayout.setFitsSystemWindows(false);
contentLayout.setClipToPadding(true);
drawer.setFitsSystemWindows(false);
}
}
private View getSwipeMenuView(ViewGroup itemView) {
if (itemView instanceof SwipeHorizontalMenuLayout) {
return itemView;
}
List<View> unvisited = new ArrayList<>();
unvisited.add(itemView);
while (!unvisited.isEmpty()) {
View child = unvisited.remove(0);
if (!(child instanceof ViewGroup)) continue;
if (child instanceof SwipeHorizontalMenuLayout) return child;
ViewGroup group = (ViewGroup) child;
int childCount = group.getChildCount();
for (int i = 0; i < childCount; i++) {
unvisited.add(group.getChildAt(i));
}
}
return itemView;
}
private static String getButtonName(View view ){
String viewName = "";
//获取文本值
if (view instanceof TextView){
viewName = ((TextView) view).getText().toString();
}else if (view instanceof ViewGroup){
ViewGroup viewGroup = (ViewGroup)view;
int count = viewGroup.getChildCount();
for (int i = 0; i < count; i++) {
View itemView = viewGroup.getChildAt(i);
String id = ViewUtils.getSimpleResourceName(itemView.getContext(), itemView.getId());
if (itemView instanceof TextView){
viewName = ((TextView) itemView).getText().toString();
break;
}else if (itemView instanceof ViewGroup){
String name =getButtonName(itemView);
if (null != name){
return name;
}
}
}
}
return viewName;
}
/**
* <p>Replace the font of specified view and it's children with specified typeface</p>
*/
private void replaceFont(@NonNull View root, @NonNull Typeface typeface) {
if (root == null || typeface == null) {
return;
}
if (root instanceof TextView) { // If view is TextView or it's subclass, replace it's font
TextView textView = (TextView)root;
// Extract previous style of TextView
int style = Typeface.NORMAL;
if (textView.getTypeface() != null) {
style = textView.getTypeface().getStyle();
}
textView.setTypeface(typeface, style);
} else if (root instanceof ViewGroup) { // If view is ViewGroup, apply this method on it's child views
ViewGroup viewGroup = (ViewGroup) root;
for (int i = 0; i < viewGroup.getChildCount(); ++i) {
replaceFont(viewGroup.getChildAt(i), typeface);
}
} // else return
}
static boolean canTextInput(View v) {
if (v.onCheckIsTextEditor()) {
return true;
}
if (!(v instanceof ViewGroup)) {
return false;
}
ViewGroup vg = (ViewGroup)v;
int i = vg.getChildCount();
while (i > 0) {
i--;
v = vg.getChildAt(i);
if (canTextInput(v)) {
return true;
}
}
return false;
}
/**
* Tests scrollability within child views of v given a delta of dx.
*
* @param v View to test for horizontal scrollability
* @param checkV Whether the view v passed should itself be checked for
* scrollability (true), or just its children (false).
* @param dx Delta scrolled in pixels along the X axis
* @param dy Delta scrolled in pixels along the Y axis
* @param x X coordinate of the active touch point
* @param y Y coordinate of the active touch point
* @return true if child views of v can be scrolled by delta of dx.
*/
protected boolean canScroll(View v, boolean checkV, int dx, int dy, int x, int y) {
if (v instanceof ViewGroup) {
final ViewGroup group = (ViewGroup) v;
final int scrollX = v.getScrollX();
final int scrollY = v.getScrollY();
final int count = group.getChildCount();
// Count backwards - let topmost views consume scroll distance
// first.
for (int i = count - 1; i >= 0; i--) {
// TODO: Add versioned support here for transformed views.
// This will not work for transformed views in Honeycomb+
final View child = group.getChildAt(i);
if (x + scrollX >= child.getLeft() && x + scrollX < child.getRight() && y + scrollY >= child
.getTop() && y + scrollY < child.getBottom() && canScroll(child, true, dx, dy,
x + scrollX - child.getLeft(), y + scrollY - child.getTop())) {
return true;
}
}
}
return checkV && (ViewCompat.canScrollHorizontally(v, -dx) || ViewCompat.canScrollVertically(v,
-dy));
}
private static void hideTipsInternal(View targetView, View tipsView) {
ViewGroup tipsContainerView = (ViewGroup) targetView.getParent();
if (!(tipsContainerView instanceof TipsContainer)) {
return;
}
tipsContainerView.removeView(tipsView);
boolean hideTarget = false;
for (int i = 0; i < tipsContainerView.getChildCount(); ++i) {
Tips tips = (Tips) tipsContainerView.getChildAt(i).getTag();
if (tips == null) {
continue;
}
hideTarget = tips.mHideTarget;
if (hideTarget) {
break;
}
}
targetView.setVisibility(hideTarget ? View.INVISIBLE : View.VISIBLE);
if (tipsContainerView.getChildCount() == 1) {
removeContainerView(tipsContainerView, targetView);
}
}
private void dumpViewHierarchy(String prefix, PrintWriter writer, View view) {
writer.print(prefix);
if (view == null) {
writer.println("null");
return;
}
writer.println(viewToString(view));
if (!(view instanceof ViewGroup)) {
return;
}
ViewGroup grp = (ViewGroup)view;
final int N = grp.getChildCount();
if (N <= 0) {
return;
}
prefix = prefix + " ";
for (int i=0; i<N; i++) {
dumpViewHierarchy(prefix, writer, grp.getChildAt(i));
}
}
public void receiveChildInfo() {
firstChild = (ViewGroup) getChildAt(0);
if(firstChild != null){
subChildCount = firstChild.getChildCount();
for(int i = 0;i < subChildCount;i++){
if(((View)firstChild.getChildAt(i)).getWidth() > 0){
pointList.add(((View)firstChild.getChildAt(i)).getLeft());
}
}
}
}
public Component findComponentOfType(Class type) {
List<ViewGroup> groups = new ArrayList<>();
groups.add(this);
while (!groups.isEmpty()) {
ViewGroup group = groups.remove(0);
for (int i = 0; i < group.getChildCount(); i++) {
View child = group.getChildAt(i);
if (child instanceof ComponentView && ((ComponentView) child).getComponent().getClass().equals(type))
return ((ComponentView) child).getComponent();
if (child instanceof ViewGroup)
groups.add((ViewGroup) child);
}
}
return null;
}
public static InterceptLayout setupRudely(View v) {
ViewGroup parent = (ViewGroup) v.getParent();
int index = 0;
ViewGroup.LayoutParams currlp = v.getLayoutParams();
for (int i = 0; i < parent.getChildCount(); i++) {
if (parent.getChildAt(i) == v) {
index = i;
break;
}
}
parent.removeView(v);
InterceptLayout layout = new InterceptLayout(v.getContext());
ViewGroup.LayoutParams lpOuter;
LinearLayout.LayoutParams lpInner;
if (currlp == null) {
lpOuter = new ViewGroup.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
lpInner = new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
} else if (currlp instanceof ViewGroup.MarginLayoutParams) {
lpOuter = currlp;
lpInner = new LayoutParams(currlp.width, currlp.height);
lpInner.bottomMargin = ((MarginLayoutParams) currlp).bottomMargin;
lpInner.topMargin = ((MarginLayoutParams) currlp).topMargin;
lpInner.leftMargin = ((MarginLayoutParams) currlp).leftMargin;
lpInner.rightMargin = ((MarginLayoutParams) currlp).rightMargin;
((MarginLayoutParams) currlp).bottomMargin = ((MarginLayoutParams) currlp).topMargin
= ((MarginLayoutParams) currlp).leftMargin = ((MarginLayoutParams) currlp).rightMargin = 0;
lpOuter.height = lpOuter.width = WRAP_CONTENT;
} else {
lpOuter = new ViewGroup.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
lpInner = new LinearLayout.LayoutParams(currlp.width, currlp.height);
}
layout.addView(v, lpInner);
parent.addView(layout, index, lpOuter);
return layout;
}
private void reset(View root) {
if (root instanceof ViewGroup) {
ViewGroup parent = (ViewGroup) root;
for (int i = 0; i < parent.getChildCount(); i++) {
reset(parent.getChildAt(i));
}
} else {
root.setScaleX(1);
root.setScaleY(1);
root.setAlpha(1);
}
}
public static void recycleViewGroupAndChildViews(ViewGroup viewGroup, boolean recycleBitmap) {
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View child = viewGroup.getChildAt(i);
if (child instanceof WebView) {
WebView webView = (WebView) child;
webView.loadUrl("about:blank");
webView.stopLoading();
continue;
}
if (child instanceof ViewGroup) {
recycleViewGroupAndChildViews((ViewGroup) child, true);
continue;
}
if (child instanceof ImageView) {
ImageView iv = (ImageView) child;
Drawable drawable = iv.getDrawable();
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
Bitmap bitmap = bitmapDrawable.getBitmap();
if (recycleBitmap && bitmap != null) {
bitmap.recycle();
}
}
iv.setImageBitmap(null);
iv.setBackground(null);
continue;
}
child.setBackground(null);
}
viewGroup.setBackground(null);
}
private void setZeroPaddingToLayoutChildren(View view) {
if (!(view instanceof ViewGroup))
return;
ViewGroup viewGroup = (ViewGroup) view;
int childCount = viewGroup.getChildCount();
for (int i = 0; i < childCount; i++) {
setZeroPaddingToLayoutChildren(viewGroup.getChildAt(i));
viewGroup.setPaddingRelative(0, viewGroup.getPaddingTop(), viewGroup.getPaddingEnd(), viewGroup.getPaddingBottom());
}
}
public void setFont(ViewGroup group, Typeface font) {
int count = group.getChildCount();
View v;
for (int i = 0; i < count; i++) {
v = group.getChildAt(i);
if (v instanceof TextView || v instanceof EditText || v instanceof Button) {
((TextView) v).setTypeface(font);
} else if (v instanceof ViewGroup)
setFont((ViewGroup) v, font);
}
}
private void failIfIndexHigherThenChildCount(ViewGroup viewGroup, int index, long endTime){
while(index > viewGroup.getChildCount()){
final boolean timedOut = SystemClock.uptimeMillis() > endTime;
if (timedOut){
int numberOfIndexes = viewGroup.getChildCount();
Assert.fail("Can not click on index " + index + " as there are only " + numberOfIndexes + " indexes available");
}
sleeper.sleep();
}
}
protected static void SetMetroFont(ViewGroup layout)
{
for (int i = 0; i < layout.getChildCount(); i++)
{
TextView text = (layout.getChildAt(i) instanceof TextView ? (TextView)layout.getChildAt(i) : null);
if (text != null)
text.setTypeface(typeface);
}
}
private void traverse(View view, List<Element> elements) {
if (UETool.getInstance().getFilterClasses().contains(view.getClass().getName())) return;
if (view.getAlpha() == 0 || view.getVisibility() != View.VISIBLE) return;
if (getResources().getString(R.string.uet_disable).equals(view.getTag())) return;
elements.add(new Element(view));
if (view instanceof ViewGroup) {
ViewGroup parent = (ViewGroup) view;
for (int i = 0; i < parent.getChildCount(); i++) {
traverse(parent.getChildAt(i), elements);
}
}
}
static int computeComponentContentHeight(@NonNull WXComponent component) {
View view = component.getHostView();
if(view == null) {
return 0;
}
if(component instanceof WXListComponent) {
WXListComponent listComponent = (WXListComponent) component;
BounceRecyclerView bounceRecyclerView = listComponent.getHostView();
if(bounceRecyclerView == null) {
return 0;
}
WXRecyclerView innerView = bounceRecyclerView.getInnerView();
if(innerView == null) {
return bounceRecyclerView.getMeasuredHeight();
} else {
return innerView.computeVerticalScrollRange();
}
} else if(component instanceof WXScroller) {
WXScroller scroller = (WXScroller) component;
if(!ViewUtils.isVerticalScroller(scroller)) {
return view.getMeasuredHeight();
}
ViewGroup group = scroller.getInnerView();
if(group == null) {
return view.getMeasuredHeight();
}
if(group.getChildCount() != 1) {
return view.getMeasuredHeight();
} else {
return group.getChildAt(0).getMeasuredHeight();
}
} else {
return view.getMeasuredHeight();
}
}