在布局中迭代视图并更改字体

cla*_*ark 7 layout android view typeface

我想知道是否有办法迭代布局中的所有视图并更改所有具有文本的视图(即TextView,CheckBox,EditText等)的字体.我有一个布局,我调用setContentView(),并想知道是否有一个简单的方法来做到这一点.

我可以通过findViewById()手动完成它,但我宁愿有一种简单的方法来迭代它们.

谢谢,-clark-

evi*_*one 18

也许这会让你开始:

protected void changeFonts(ViewGroup root) {

        Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/comicsans.ttf");

        for(int i = 0; i <root.getChildCount(); i++) {
                View v = root.getChildAt(i);
                if(v instanceof TextView ) {
                        ((TextView)v).setTypeface(tf);
                } else if(v instanceof Button) {
                        ((Button)v).setTypeface(tf);
                } else if(v instanceof EditText) {
                        ((EditText)v).setTypeface(tf);
                } else if(v instanceof ViewGroup) {
                        changeFonts((ViewGroup)v);
                }
        }

    }
Run Code Online (Sandbox Code Playgroud)