Java 检测所有小部件的Android实例?
我试图通过迭代Java 检测所有小部件的Android实例?,java,android,views,instanceof,Java,Android,Views,Instanceof,我试图通过迭代线性布局的视图并使用实例来设置所有文本视图的字体 该表单目前由4个文本视图和一个按钮组成 下面的代码正在检测所有视图s,甚至按钮作为文本视图 /* Set fonts */ LinearLayout ll = (LinearLayout) findViewById(R.id.ll_screenincourse_wrapper); for (int i = 0; i < ll.getChildCount(); i++) {
线性布局
的视图并使用实例来设置所有文本视图
的字体
该表单目前由4个文本视图
和一个按钮
组成
下面的代码正在检测所有视图
s,甚至按钮
作为文本视图
/* Set fonts */
LinearLayout ll = (LinearLayout) findViewById(R.id.ll_screenincourse_wrapper);
for (int i = 0; i < ll.getChildCount(); i++) {
View v = ll.getChildAt(i);
if (v instanceof TextView) {
((TextView) v).setTypeface(Fonts.get3dDumbFont(this));
}
}
是不是因为按钮和文本视图都是视图类型
?如果是这样,那么正确的方法是什么
谢谢你的帮助。Ricky.实际上,Button是TextView的一个子类!这就是为什么您将其视为文本视图(它也是文本视图)
如果instanceof
排除按钮或使用
if (v.getClass() == TextView.class)
但这与TextView的任何其他子类都不匹配(如果您使用它们)。这很简单
延伸
检查
public class Button extends TextView
if (v.getClass() == TextView.class)