我正在学习如何更改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>
有人帮我吗?我的代码错了吗?谢谢..对不起我的英语