在三星手机上调试我的应用程序时出现奇怪的结果,我没有物理访问权限。我想请用户运行一个检测的应用程序来帮助调试。我的应用程序获得一个view
,其中包含一个未知的(对我来说: - )层次结构(ViewGroups
等)。有没有办法“走”View
并打印到字符串/ ddms
View
中的所有组件(等等ViewGroups
等)?
这类似于HierarchyViewer
工具 - 如果我有adb
级别的设备访问权限。
更新:我想我可以使用方法
void dumpViewHierarchyWithProperties(Context context, ViewGroup group, BufferedWriter out, int level)
来自ViewDebug.java
Android操作系统资源......
任何人都有更好的主意吗?
谢谢!
答案 0 :(得分:31)
这是我刚为此目的而设的实用功能:
public static void printViewHierarchy(ViewGroup vg, String prefix) {
for (int i = 0; i < vg.getChildCount(); i++) {
View v = vg.getChildAt(i);
String desc = prefix + " | " + "[" + i + "/" + (vg.getChildCount()-1) + "] "+ v.getClass().getSimpleName() + " " + v.getId();
Log.v("x", desc);
if (v instanceof ViewGroup) {
printViewHierarchy((ViewGroup)v, desc);
}
}
}
答案 1 :(得分:10)
我创建了实用工具方法,它以漂亮的印刷方式返回层次结构,并带有人类可读的视图ID。以下是输出的示例:
[LinearLayout] no_id
[CardView] com.example:id/card_view
[RelativeLayout] no_id
[LinearLayout] com.example:id/image_container
[AppCompatImageView] com.example:id/incident_icon
[CustomTextView] com.example:id/road_number
[RelativeLayout] no_id
[CustomTextView] com.example:id/distance_to_user
[CustomTextView] com.example:id/obstruction_title
[CustomTextView] com.example:id/road_direction
[CustomTextView] com.example:id/obstruction_description
[AppCompatImageView] com.example:id/security_related
这是实用方法:
public static String getViewHierarchy(@NonNull View v) {
StringBuilder desc = new StringBuilder();
getViewHierarchy(v, desc, 0);
return desc.toString();
}
private static void getViewHierarchy(View v, StringBuilder desc, int margin) {
desc.append(getViewMessage(v, margin));
if (v instanceof ViewGroup) {
margin++;
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
getViewHierarchy(vg.getChildAt(i), desc, margin);
}
}
}
private static String getViewMessage(View v, int marginOffset) {
String repeated = new String(new char[marginOffset]).replace("\0", " ");
try {
String resourceId = v.getResources() != null ? (v.getId() > 0 ? v.getResources().getResourceName(v.getId()) : "no_id") : "no_resources";
return repeated + "[" + v.getClass().getSimpleName() + "] " + resourceId + "\n";
} catch (Resources.NotFoundException e) {
return repeated + "[" + v.getClass().getSimpleName() + "] name_not_found\n";
}
}
提示:我们使用此方法将视图层次结构添加到某些崩溃报告中。 在某些情况下,它确实非常有用。
答案 2 :(得分:0)
这些视图不是我的,它们来自其他应用程序,因此我无法触及与它们相关的代码(我正在从RemoteView中对它们进行充气)
如果您收到RemoteViews
,并且将其应用于活动中的某些内容,则可以直接访问生成的Views
,与您自己从XML中对其进行充气的情况完全不同。鉴于根View
,它只是走路的孩子(可能是深度优先,但这是你的电话)并记录你想要的任何信息。
答案 3 :(得分:0)
与this answer几乎相同,但与Kotlin:
fun getViewTree(root: ViewGroup): String{
fun getViewDesc(v: View): String{
val res = v.getResources()
val id = v.getId()
return "[${v::class.simpleName}]: " + when(true){
res == null -> "no_resouces"
id > 0 -> try{
res.getResourceName(id)
} catch(e: android.content.res.Resources.NotFoundException){
"name_not_found"
}
else -> "no_id"
}
}
val output = StringBuilder(getViewDesc(root))
for(i in 0 until root.getChildCount()){
val v = root.getChildAt(i)
output.append("\n").append(
if(v is ViewGroup){
getViewTree(v).prependIndent(" ")
} else{
" " + getViewDesc(v)
}
)
}
return output.toString()
}
答案 4 :(得分:0)
只需从BottomNavigationView中找到菜单项并注册长按即可。
View itemBag = bottomNavigationView.findViewById(R.id.action_bag);
if (itemBag != null) {
itemBag.setOnLongClickListener(BottomNavigationActivity.this::onLongClick);
}
private boolean onLongClick(View v) {
switch (v.getId()) {
case R.id.action_bag: {
showToastMessage(R.string.menu_shopping_bag);
}
break;
}
return false;
}
答案 5 :(得分:0)
其他输出的格式令我不满意,有些地方的代码令我不满意。因此,我通过以下方式改进了输出:
/**
* Prints the view hierarchy.
*/
public static void printViewHierarchy(ViewGroup parent, String intent) {
for (int i = 0, max = parent.getChildCount(), numLenght = (max + "").length(); i < max; i++) {
View child = parent.getChildAt(i);
String childString = child.getClass().getSimpleName() + " " + child.getId();
String format = "|— %0" + numLenght + "d/%0" + numLenght + "d %s";
Log.d("debug", intent + String.format(format, i + 1, max, childString));
if (child instanceof ViewGroup)
printViewHierarchy((ViewGroup) child, intent + " ");
}
}
收件人:
|— 1/4 ScrollView 2131296482
|— 2/4 MaterialTextView 2131296445
|— 3/4 FloatingActionButton 2131296374
|— 4/4 MaterialTextView 2131296363
|— 1/4 ScrollView 2131296482
|— 1/1 LinearLayout 2129243036
|— 01/74 RelativeLayout 2131296449
|— 1/4 MaterialCheckBox 2131296332
|— 2/4 MaterialTextView 2131296547
|— 3/4 MaterialTextView 2131296531
|— 4/4 AppCompatImageButton 2131296462
|— 02/74 RelativeLayout 2131296449
|— 1/4 MaterialCheckBox 2131296332
|— 2/4 MaterialTextView 2131296547
|— 3/4 MaterialTextView 2131296531
|— 4/4 AppCompatImageButton 2131296462
|— ...
|— 74/74 RelativeLayout 2131296449
|— 1/4 MaterialCheckBox 2131296332
|— 2/4 MaterialTextView 2131296547
|— 3/4 MaterialTextView 2131296531
|— 4/4 AppCompatImageButton 2131296462
|— 2/4 MaterialTextView 2131296445
|— 3/4 FloatingActionButton 2131296374
|— 4/4 MaterialTextView 2131296363