编写不易,如有转载,请声明出处: 梦回河口:http://blog.csdn.net/zxc514257857/article/details/73480651
android:typeface="xxx"
Typeface typeface = Typeface.createFromAsset(getAssets(), "heijian.ttf");
全局修改的文字类型只有TextView和EditText两种,即除了EditText控件及其相关控件之外的所有类型的字体都为TextView类型
// BaseActivity
import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.view.LayoutInflaterCompat;
import android.support.v4.view.LayoutInflaterFactory;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class BaseActivity extends AppCompatActivity {
public static Typeface typeface1;
public static Typeface typeface2;
@Override
protected void onCreate(Bundle savedInstanceState) {
if (typeface1 == null) {
typeface1 = Typeface.createFromAsset(getAssets(), "yrdzst.ttf");
}
if(typeface2 == null){
typeface2 = Typeface.createFromAsset(getAssets(), "heijian.ttf");
}
LayoutInflaterCompat.setFactory(LayoutInflater.from(this), new LayoutInflaterFactory() {
@Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
AppCompatDelegate delegate = getDelegate();
View view = delegate.createView(parent, name, context, attrs);
// 如果控件文字类型属于TextView,则加载第一种字体
if (view != null && (view instanceof TextView)) {
((TextView) view).setTypeface(typeface1);
}
// 如果控件文字类型属于EditText,则加载第二种字体
if(view !=null && (view instanceof EditText)){
((EditText) view).setTypeface(typeface2);
}
return view;
}
});
super.onCreate(savedInstanceState);
}
}
---------------------------------------------------------------------------------------------------
// MainActivity
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends BaseActivity {
private List mArrList;
private List mList;
private RecyclerView mRecyclerView;
private ListView mListView;
private Context mContext = MainActivity.this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
public void initView(){
mListView = (ListView) findViewById(R.id.listview);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
}
public void initData(){
mArrList = new ArrayList<>();
mList = new ArrayList<>();
mArrList.add("一人 我饮酒醉");
mArrList.add("醉把佳人成双对");
mArrList.add("两眼 是独相随");
mArrList.add("我只求他日能双归");
mArrList.add("娇女 我轻扶琴");
mArrList.add("燕嬉 我紫竹林");
mArrList.add("我痴情红颜");
mArrList.add("我心甘情愿");
mArrList.add("我千里把君寻");
mList.add("说红颜 我痴情笑");
mList.add("曲动 我琴声妙");
mList.add("我轻狂高傲");
mList.add("我懵懂无知");
mList.add("我只怪太年少");
mList.add("弃江山 我忘天下");
mList.add("斩断情丝我无牵挂");
mList.add("千古留名传佳话");
mList.add("两年征战已白发");
mList.add("一生征战何人陪");
mList.add("我谁是谁非我谁相随");
mList.add("戎马一生为了谁");
mList.add("能爱几回我恨几回");
mList.add("败帝王 我斗苍天");
mList.add("我夺得皇位以成仙");
mList.add("豪情万丈天地间");
mList.add("续写另类我帝王篇");
mListView.setAdapter(new MyListViewAdapter(mContext , R.layout.item_listview, mArrList));
mRecyclerView.setAdapter(new MyRecyclerViewAdapter(mContext , R.layout.item_recyclerview, mList));
mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
}
}
---------------------------------------------------------------------------------------------------
// MyListViewAdapter
import android.content.Context;
import android.graphics.Typeface;
import android.widget.TextView;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;
import java.util.List;
class MyListViewAdapter extends CommonAdapter {
private Typeface mFromAsset;
MyListViewAdapter(Context context, int layoutId, List datas) {
super(context, layoutId, datas);
mFromAsset = Typeface.createFromAsset(mContext.getAssets(), "yrdzst.ttf");
}
@Override
protected void convert(ViewHolder viewHolder, String item, int position) {
TextView textView= viewHolder.getView(R.id.textview_listview);
textView.setTypeface(mFromAsset);
viewHolder.setText(R.id.textview_listview , item);
}
}
---------------------------------------------------------------------------------------------------
// MyRecyclerViewAdapter
import android.content.Context;
import android.graphics.Typeface;
import android.widget.TextView;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;
import java.util.List;
class MyRecyclerViewAdapter extends CommonAdapter {
private Typeface mFromAsset;
MyRecyclerViewAdapter(Context context, int layoutId, List datas) {
super(context, layoutId, datas);
mFromAsset = Typeface.createFromAsset(mContext.getAssets(), "yrdzst.ttf");
}
@Override
protected void convert(ViewHolder holder, String s, int position) {
TextView textView= holder.getView(R.id.textview_recyclerview);
textView.setTypeface(mFromAsset);
holder.setText(R.id.textview_recyclerview , s);
}
}
---------------------------------------------------------------------------------------------------
// activity_main.xml
"1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.globalchangetext.MainActivity">
"@+id/listview"
android:clickable="true"
android:divider="#30707070"
android:layout_width="match_parent"
android:layout_height="200dp"
android:dividerHeight="10dp"
android:scrollbars="none"
android:overScrollMode="never"/>
.support.v7.widget.RecyclerView
android:layout_marginTop="20dp"
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scrollbars="none"
android:overScrollMode="never"/>
"@+id/edittext"
android:text="一人我饮酒醉"
android:gravity="center"
android:padding="10dp"
android:textSize="22sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
---------------------------------------------------------------------------------------------------
// item_listview.xml
"1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:id="@+id/textview_listview"
android:gravity="center"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"/>
---------------------------------------------------------------------------------------------------
// item_recyclerview.xml
"1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:id="@+id/textview_recyclerview"
android:gravity="center"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"/>
注1:想要修改字体的Activity直接继承BaseActivity即可,如果想要再细化修改,再到相应的Activity中使用Typeface.createFromAsset方式修改字体
注2:需要在module的build.gradle中的dependencies节点下加入:
compile 'com.zhy:base-adapter:3.0.3'
compile 'com.zhy:base-rvadapter:3.0.3'
注3:字体文件需在和java文件平级的目录中新建assets文件夹,并将字体文件放入其中,这样加载字体:
Typeface typeface = Typeface.createFromAsset(getAssets(), "heijian.ttf");
Demo下载请移步:http://download.csdn.net/detail/zxc514257857/9875112
———-因本人才疏学浅,如博客或Demo中有错误的地方请大家随意指出,与大家一起讨论,共同进步,谢谢!———-