背景
在android布局文件中,有很多UI元素和视图组。有时,我们不需要为视图提供id值(唯一标识符)。在这种情况下,我们不能通过说findViewByid()来找到视图。因此,我们不能操纵他们。
问题是
如何为任何活动的所有视图生成路径,示例如下:
content>LinearLayout-0>RelativeLayout-3>LinearLayout-0>TextView-2
以上线的意思是
基本上,我在寻找如下函数:
String path = getViewPath(view);和
View view = findViewByPath(path)用例:
实际上,服务器将通过修改查看路径向移动应用程序广播一些命令,
然后,移动应用程序将从路径中找到视图并更改视图的属性。
发布于 2017-09-18 13:14:01
在上述问题的解决方案下面,我创建了获取视图路径的方法和逐路径获取视图的方法。
干杯!
package com.test;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.Window;
import java.util.Arrays;
public class CustomViewIdManager {
private static final String ACTIVITY_CLASS_SEPARATOR = "@";
private static final String VIEW_SEPARATOR = ">";
private static final String VIEW_POSITION_SEPARATOR = ":";
private static final String MAIN_CONTENT_LAYOUT_NAME = "content";
/**
* Find given view path in activity hierarchy
*
* @param view
* @param activity
* @return Path given view
*/
public static String generateViewPathInActivityViewHierarchy(View view, Activity activity) {
String path = "";
View currentView = view;
ViewParent currentParent;
do {
currentParent = currentView.getParent();
if (currentView.getId() == Window.ID_ANDROID_CONTENT) {
path = activity.getLocalClassName() + ACTIVITY_CLASS_SEPARATOR + MAIN_CONTENT_LAYOUT_NAME + path;
break;
} else {
path = VIEW_SEPARATOR + currentView.getClass().getSimpleName() + VIEW_POSITION_SEPARATOR + getSelfIndexInParent((View) currentParent, currentView) + path;
}
currentView = (View) currentView.getParent();
} while (true);
return path;
}
/**
* Finding the view by given path in activity view hierarchy
* @param path
* @param activity
* @return
*/
public static View findViewByCustomPath(String path, Activity activity) {
String[] activitySplitting = path.split(ACTIVITY_CLASS_SEPARATOR);
String[] viewSplitting = activitySplitting[1].split(VIEW_SEPARATOR);
View viewLooker = null;
if (viewSplitting[0].equalsIgnoreCase(MAIN_CONTENT_LAYOUT_NAME)) {
viewLooker = ViewUtil.getContentView(activity);
}
return viewFinder(viewLooker, Arrays.copyOfRange(viewSplitting, 1, viewSplitting.length));
}
public static View viewFinder(View view, String[] restPath) {
View viewToSendBack;
String singleView = restPath[0];
String[] viewPositioningSplitting = singleView.split(VIEW_POSITION_SEPARATOR);
viewToSendBack = ((ViewGroup) view).getChildAt(Integer.parseInt(viewPositioningSplitting[1]));
if (restPath.length > 1) {
return viewFinder(viewToSendBack, Arrays.copyOfRange(restPath, 1, restPath.length));
} else {
return viewToSendBack;
}
}
/**
* This will calculate the self position inside view
*
* @param parent
* @param view
* @return index of child
*/
public static int getSelfIndexInParent(View parent, View view) {
int index = -1;
if (parent instanceof ViewGroup) {
ViewGroup viewParent = (ViewGroup) parent;
for (int i = 0; i < viewParent.getChildCount(); ++i) {
View child = viewParent.getChildAt(i);
++index;
if (child == view) {
return index;
}
}
}
return index;
}
}发布于 2017-09-15 09:59:41
我认为最简单的解决方案是使用ViewGroup的getChildAt()和getChildCount()方法。
因此,您可以将所需的每个View转换为一个ViewGroup,并调用上述方法。不过,这不会返回String路径。但是,您仍然可以获得一个相对层次结构,并以您的方式达到特定的用例。
https://stackoverflow.com/questions/46236606
复制相似问题