当前位置: 首页 > 面试题库 >

遍历GroupView的所有子项?

司寇羽
2023-03-14
问题内容

这是我的代码:

    Typeface font = Typeface.createFromAsset(getAssets(), "fonts/choco.ttf");  
    ViewGroup rootView = (ViewGroup) findViewById(android.R.id.content).getRootView();
    for(int i = 0; i < rootView.getChildCount(); i++)
    {
        View child = rootView.getChildAt(i);
        Log.d("menfis", child.toString());
        if(child != null)
            if(child.getClass() == TextView.class)
                ((TextView) child).setTypeface(font);
    }

这是我此页面的XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background_page"
android:gravity="center_horizontal" >

<ImageView
    android:id="@+id/imgApplicationIcon"         
    android:src="@drawable/logo" 
    android:layout_width="fill_parent" android:layout_height="wrap_content"/>

<TextView  
    android:layout_marginTop="40dip"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="right"
    android:textColor="@color/red"
    android:lines="2"
    android:layout_marginRight="30dip"
    android:text="test1" />

<TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="right"
    android:layout_marginRight="30dip"
    android:textColor="@color/red"
    android:lines="2"
    android:text="test2" />
</LinearLayout>

问题是我只能在LogCat控制台上得到它:

09-20 14:34:27.264: DEBUG/menfis(12251): android.widget.LinearLayout@40523820

为什么我没有得到对TextViews的引用?


问题答案:

您需要递归遍历视图树。当前,您仅列出根视图的子级。

换句话说,您需要这样的东西:

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ...
    Typeface font = Typeface.createFromAsset(getAssets(), "fonts/choco.ttf");  
    ViewGroup rootView = (ViewGroup) findViewById(android.R.id.content).getRootView();
    applyFontRecursively(rootView, font);
}

void applyFontRecursively(ViewGroup parent, Typeface font) 
{    
    for(int i = 0; i < parent.getChildCount(); i++)
    {
        View child = parent.getChildAt(i);            
        if(child instanceof ViewGroup) 
        {
            applyFontRecursively((ViewGroup)child, font);
        }
        else if(child != null)
        {
            Log.d("menfis", child.toString());
            if(child.getClass() == TextView.class)
            {
                ((TextView) child).setTypeface(font);
            }
        }                
    }
}


 类似资料:
  • 问题内容: 我有一个带有多个子SKSpriteNode的SKNode。一个简化的例子: 我想对所有这些孩子采取行动。当我在上执行操作时,没有任何效果。 我不能在父项上使用,因为它的许多子项已经具有我正在使用的名称。 有没有一种方法可以遍历的所有子项,以便对所有子项执行单个操作? 问题答案: 您可以简单地列举: 如有必要,请检查每个孩子是否确实是: 从 Swift 2(Xcode 7)开始, 可以将

  • 问题内容: 我正在运行以下代码,从具有特定列的所有表中提取所有相关的行。外层应该检查该迭代表中是否存在该列。如果没有,它应该完成该迭代并移至下一张表。如果表中有该列,则应检查该表是否将返回任何记录。如果没有要返回的记录,则应结束该迭代并移至下一个表。如果有记录,则应在SSMS中显示它们。 这似乎可行,因为SSMS仅返回带有有效条目的网格。我不明白的是:为什么我仍然会收到这些错误? 编辑 使用建议后

  • 问题内容: 有没有一种方法可以获取Go语言映射中所有键的列表?元素的数量由给出,但是如果我有类似的地图: 如何遍历所有键? 问题答案: https://play.golang.org/p/JGZ7mN0-U- 要么 语句的Go语言规范指定第一个值是键,第二个变量是值,但不必存在。

  • 问题内容: 如何使用pathlib递归遍历给定目录的所有子目录? 似乎只迭代给定目录的直接子级。 我知道这可以通过或使用,但是我想使用pathlib,因为我喜欢使用path对象。 问题答案: 您可以使用对象的方法:

  • 问题内容: 我们有一个软件不会删除不再需要的条目。为了了解服务器中有多少数据正在浪费,并准备进行大型清理操作,我试图遍历所有表并拉出标记为删除的记录。这就是我正在使用的: 这是我的错误: 消息116,级别16,状态1,行19当未将EXISTS引入子查询时,只能在选择列表中指定一个表达式。 我意识到我的错误可能与选择该行中的两列有关 但是,我不确定如何才能确保选择下一行。 PS仅用于脚本测试。 如何

  • 问题内容: 我有一个表A,并且有一个主键ID。 现在我要遍历A中的所有行。 我发现类似“针对A中的每个记录”的内容,但这似乎并不是您在MySQL中的处理方式。 我想为每一行获取一个字段并对其进行转换,将其插入到另一个表中,然后更新该行的某些字段。我可以将select部分和insert放入一个语句中,但是我也不知道如何在其中进行更新。所以我想循环。实际上,我不想使用MySQL以外的任何东西。 编辑