- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
背景
在 android 布局文件中有很多 UI 元素和 View 组。有时我们不需要为 View 提供 id
值(唯一标识符)。在这种情况下,我们无法通过调用 findViewByid()
找到 View 。因此我们无法操纵它们。
问题是
我们如何为任何 Activity 的所有 View 生成路径,示例如下:
content>LinearLayout-0>RelativeLayout-3>LinearLayout-0>TextView-2
上面一行的意思是
所以基本上我正在寻找如下功能:
String path = getViewPath(view);
和
View view = findViewByPath(path)
用例:
实际上服务器会通过指定 View 路径向移动应用程序广播一些命令,
然后移动应用程序将从路径中找到 View 并更改 View 的属性
最佳答案
在上述问题的解决方案下方,我创建了获取 View 路径和按路径获取 View 的方法。
干杯!!!
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;
}
}
关于Android - 任何 Activity 的 Root View 层次结构中存在的所有 View 的相对路径生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46236606/
我正在尝试将多个水平链接的 Button 和 TextView 垂直链接为 View 集,但仍保持平面 View 层次结构。这是我的初始布局和代码:
到目前为止,我已经在Google BigQuery上训练了几种模型,目前我需要查看模型的外观(即架构,损失函数等)。 有没有办法获取这些信息? 最佳答案 仔细阅读文档后,我可以说该功能尚不存在。我什至
本文实例讲述了PHP实现二叉树深度优先遍历(前序、中序、后序)和广度优先遍历(层次)。分享给大家供大家参考,具体如下: 前言: 深度优先遍历:对每一个可能的分支路径深入到不能再深入为止,而且每个
我是一名优秀的程序员,十分优秀!