Loop through all childs of GroupView?

后端 未结 1 689
抹茶落季
抹茶落季 2020-12-06 18:21

Here is my code:

    Typeface font = Typeface.createFromAsset(getAssets(), \"fonts/choco.ttf\");  
    ViewGroup rootView = (ViewGroup) findViewById(android.         


        
相关标签:
1条回答
  • 2020-12-06 18:52

    You need to traverse View tree recursively. Currently you only list children of root view.

    In other words, you need something like this:

    @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);
                }
            }                
        }
    }
    
    0 讨论(0)
提交回复
热议问题