0

我正在学习如何更改android中的所有字体,但这似乎不起作用,只是一个更改了textview,而不是全部..

这是我的代码

 public class AndroidTypefaceUtility 
{
    AndroidTypefaceUtility()
    {
    }
    public static void setFont(Context context, ViewGroup group) {
        Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/OpenSans-Light.ttf");
        int count = group.getChildCount();
        View v;
        for(int i = 0; i < count; i++) {
            v = group.getChildAt(i);
            if(v instanceof TextView || v instanceof Button || v instanceof EditText/*etc.*/)
                ((TextView)v).setTypeface(tf);
            else if(v instanceof ViewGroup)
                setFont(context, (ViewGroup)v);
        }
    }

    }

此实现代码更改所有字体

 public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState); 

            setContentView(R.layout.layout_tab);
            ViewGroup root = (ViewGroup)findViewById(R.id.myrootlayout);        
            AndroidTypefaceUtility.setFont(getApplicationContext(),root);
}

更新

这是我的xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:id="@+id/myrootlayout"
    >

    <ListView 
        android:id="@+id/menu_content_menulist"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="8dp"
        android:background="#d0000000"
        android:cacheColorHint="@android:color/transparent" 
        android:divider="@null"
        android:scrollbars="none"
        android:listSelector="@null"/>      
    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <com.smart.tangsel.side_menu_scroll.ScrollerLinearLayout        
            android:id="@+id/menu_content_side_slide_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"        
            android:orientation="vertical">
<TabHost
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center_vertical"
    android:gravity="center"
    >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/content_tab"        
        android:orientation="vertical"
        android:background="@drawable/bg_app"
         >

        <include layout="@layout/layout_action_bar" />

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@null"
            android:orientation="vertical" >          

            <FrameLayout
                android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
                android:background="@null" 
                 android:layout_above="@android:id/tabs"/>
              <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:tabStripLeft="@drawable/tabstrip"
                android:tabStripRight="@drawable/tabstrip"
                android:soundEffectsEnabled="false"
                android:layout_alignParentBottom="true"
                 />
        </RelativeLayout>

       <!-- <include layout="@layout/footer_layout" /> --> 
    </LinearLayout>

</TabHost>
</com.smart.tangsel.side_menu_scroll.ScrollerLinearLayout>

</RelativeLayout></RelativeLayout>

有人帮我吗?我的代码错了吗?谢谢..对不起我的英语

4

1 回答 1

0

尝试这个:

public static ViewGroup setFont(Context context, ViewGroup group, Typeface font) 
    {
        int count = group.getChildCount();
        View v = null;
        for(int i = 0; i < count; i++) 
        {
            v = group.getChildAt(i);
            if(v instanceof TextView || v instanceof Button || v instanceof EditText/*etc.*/)
                ((TextView)group.getChildAt(i)).setTypeface(font);
            else if(v instanceof ViewGroup)
                setFont(context, (ViewGroup)v, font);
        }

        return group;
    }

然后在你的主编辑中调用这样的东西:

protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //--Get font style to be applied from assets folder--
        Typeface font =Typeface.createFromAsset(getAssets(), "Sansation_Bold_Italic.ttf");

         ViewGroup root = (ViewGroup)findViewById(R.id.myrootlayout);        
         root = (ViewGroup) AndroidTFFActivity.setFont(this,root, font);
    }

此外,请确保您在清单中设置了正确的权限来读取文件。我拿了你的样本并成功运行它,如果这有帮助,请告诉我。

这是我用来测试的 xml 示例:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" 
    android:id="@+id/myrootlayout">


         <TextView
            android:id="@+id/txtTest1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="test  1" />

         <TextView
            android:id="@+id/txtTest2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="test  2" />

         <TextView
            android:id="@+id/txtTest3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="test 3" />


</LinearLayout>
于 2013-10-29T19:53:31.677 回答