我正在将视图记录到logcat以区分不同的视图以进行调试.
我注意到的是输出(主要由View.toString引起)是这样的:
com.example.app.CustomView{7b14550 IFE...C.. ........ 0,0-1440,315}
Run Code Online (Sandbox Code Playgroud)
花括号之间的每个部分是什么意思?
更新:只是认为答案可能在View源代码中并看了一下.对于任何想要了解这一点的人来说,都有一个toString()方法来解释每个值.
当你将.toString()调用到任何给定的类时,除非被覆盖,否则它将调用Object中的toString.每个类都以某种或其他方式将Object作为根.如果扩展一个类,那么该类的超类就是Object.如果类扩展扩展了另一个类,那么该类的超类是Object.你明白这个想法.所以从Object文档中:
/**
* Returns a string representation of the object. In general, the
* {@code toString} method returns a string that
* "textually represents" this object. The result should
* be a concise but informative representation that is easy for a
* person to read.
* It is recommended that all subclasses override this method.
* <p>
* The {@code toString} method for class {@code Object}
* returns a string consisting of the name of the class of which the
* object is an instance, the at-sign character `{@code @}', and
* the unsigned hexadecimal representation of the hash code of the
* object. In other words, this method returns a string equal to the
* value of:
* <blockquote>
* <pre>
* getClass().getName() + '@' + Integer.toHexString(hashCode())
* </pre></blockquote>
*
* @return a string representation of the object.
*/
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
Run Code Online (Sandbox Code Playgroud)
除非你覆盖它,否则它会打印出来.这意味着它打印出类'哈希码的十六进制字符串.
因此,除非被覆盖以打印出其他东西,否则它会打印出类'哈希码的十六进制字符串.
但是在View类的情况下,这是它具有的方法:
public String toString() {
StringBuilder out = new StringBuilder(128);
out.append(getClass().getName());
out.append('{');
out.append(Integer.toHexString(System.identityHashCode(this)));
out.append(' ');
switch (mViewFlags&VISIBILITY_MASK) {
case VISIBLE: out.append('V'); break;
case INVISIBLE: out.append('I'); break;
case GONE: out.append('G'); break;
default: out.append('.'); break;
}
out.append((mViewFlags&FOCUSABLE_MASK) == FOCUSABLE ? 'F' : '.');
out.append((mViewFlags&ENABLED_MASK) == ENABLED ? 'E' : '.');
out.append((mViewFlags&DRAW_MASK) == WILL_NOT_DRAW ? '.' : 'D');
out.append((mViewFlags&SCROLLBARS_HORIZONTAL) != 0 ? 'H' : '.');
out.append((mViewFlags&SCROLLBARS_VERTICAL) != 0 ? 'V' : '.');
out.append((mViewFlags&CLICKABLE) != 0 ? 'C' : '.');
out.append((mViewFlags&LONG_CLICKABLE) != 0 ? 'L' : '.');
out.append((mViewFlags&CONTEXT_CLICKABLE) != 0 ? 'X' : '.');
out.append(' ');
out.append((mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0 ? 'R' : '.');
out.append((mPrivateFlags&PFLAG_FOCUSED) != 0 ? 'F' : '.');
out.append((mPrivateFlags&PFLAG_SELECTED) != 0 ? 'S' : '.');
if ((mPrivateFlags&PFLAG_PREPRESSED) != 0) {
out.append('p');
} else {
out.append((mPrivateFlags&PFLAG_PRESSED) != 0 ? 'P' : '.');
}
out.append((mPrivateFlags&PFLAG_HOVERED) != 0 ? 'H' : '.');
out.append((mPrivateFlags&PFLAG_ACTIVATED) != 0 ? 'A' : '.');
out.append((mPrivateFlags&PFLAG_INVALIDATED) != 0 ? 'I' : '.');
out.append((mPrivateFlags&PFLAG_DIRTY_MASK) != 0 ? 'D' : '.');
out.append(' ');
out.append(mLeft);
out.append(',');
out.append(mTop);
out.append('-');
out.append(mRight);
out.append(',');
out.append(mBottom);
final int id = getId();
if (id != NO_ID) {
out.append(" #");
out.append(Integer.toHexString(id));
final Resources r = mResources;
if (id > 0 && Resources.resourceHasPackage(id) && r != null) {
try {
String pkgname;
switch (id&0xff000000) {
case 0x7f000000:
pkgname="app";
break;
case 0x01000000:
pkgname="android";
break;
default:
pkgname = r.getResourcePackageName(id);
break;
}
String typename = r.getResourceTypeName(id);
String entryname = r.getResourceEntryName(id);
out.append(" ");
out.append(pkgname);
out.append(":");
out.append(typename);
out.append("/");
out.append(entryname);
} catch (Resources.NotFoundException e) {
}
}
}
out.append("}");
return out.toString();
}
Run Code Online (Sandbox Code Playgroud)
打印出来的意思是:
`7b14550` - hash code identifying the view
`I` - the view is invisible
`F` - the view is focusable
`E` - the view is enabled
`C` - the view is clickable
`0,0-1440,315` indicates left, top, right and bottom offset in the screen.
Run Code Online (Sandbox Code Playgroud)
但是从上面的代码中可以看出,有许多不同的键需要注意.
括号用于指导您打印出的详细信息是否与您调用的.toString的视图相关联.据我所知,他们没有任何功能在外面向你显示打印出的文本属于那个类.
您可以覆盖toString并使其打印出您想要的任何数据:字段,方法结果,或者什么都不做
下次你想知道方法的作用时,CTRL +左键单击有问题的方法/字段.这将打开目标类,并向您显示开发人员留下的方法和任何可能的文档.
| 归档时间: |
|
| 查看次数: |
618 次 |
| 最近记录: |