Android中自定义LayoutInflater.Factory实现全局替换系统控件(比如TextView,EditText等)
2017-07-08 17:33
671 查看
我已写了DEMO放到github , 地址:https://github.com/chenglin198751/InflaterFactoryDemo/tree/master
我们有时会遇到这么一个需求,就是要把全局的TextView 、 EditText 替换为自定义的TextView、EditText(比如自定义字体、或者自定义emoji表情时)。如果手动一个个去替换,那不累死了。那有没有一个方法可以全局替换呢?其实安卓系统已经给我们提供了这么一个方法,只需几行代码就能做到。
1、假设你已经写好了你的自定义TextView,名字叫CustomTextView 。那么首先我们要自定义一个LayoutInflater.Factory ,代码如下:
2、再你的BaseActivity里面重写getSystemService(String name) 方法,代码如下:
3、有时开发者也会调用Application里面的getSystemService(String name)方法。那么为了保险起见,我们也把自己的Application里面的getSystemService(String name)方法重写了,代码跟上面一样,只不过是写在Application里面:
OK ,完工,现在在你的业务层Activity里面去打印日志,看看是不是很惊喜:
如果你觉得帮到了你,请给作者打赏一口饭吃:
我们有时会遇到这么一个需求,就是要把全局的TextView 、 EditText 替换为自定义的TextView、EditText(比如自定义字体、或者自定义emoji表情时)。如果手动一个个去替换,那不累死了。那有没有一个方法可以全局替换呢?其实安卓系统已经给我们提供了这么一个方法,只需几行代码就能做到。
1、假设你已经写好了你的自定义TextView,名字叫CustomTextView 。那么首先我们要自定义一个LayoutInflater.Factory ,代码如下:
public class MyLayoutInflaterFactory implements LayoutInflater.Factory { @Override public View onCreateView(String name, Context context, AttributeSet attrs) { if (name.equals("TextView")) { return new CustomTextView(context, attrs); } return null; } }
2、再你的BaseActivity里面重写getSystemService(String name) 方法,代码如下:
public class BaseActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public Object getSystemService(String name) { if (name.equals(Context.LAYOUT_INFLATER_SERVICE)) { LayoutInflater inflater = (LayoutInflater) super.getSystemService(name); if (inflater.getFactory() == null) { inflater.setFactory(new MyLayoutInflaterFactory()); } return inflater; } return super.getSystemService(name); } }
3、有时开发者也会调用Application里面的getSystemService(String name)方法。那么为了保险起见,我们也把自己的Application里面的getSystemService(String name)方法重写了,代码跟上面一样,只不过是写在Application里面:
public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); } @Override public Object getSystemService(String name) { if (name.equals(Context.LAYOUT_INFLATER_SERVICE)) { LayoutInflater inflater = (LayoutInflater) super.getSystemService(name); if (inflater.getFactory() == null) { inflater.setFactory(new MyLayoutInflaterFactory()); } return inflater; } return super.getSystemService(name); } }
OK ,完工,现在在你的业务层Activity里面去打印日志,看看是不是很惊喜:
mTextView = (TextView) findViewById(R.id.text_view); if (mTextView instanceof CustomTextView) { Log.v("tag_2", "CustomTextView"); }
如果你觉得帮到了你,请给作者打赏一口饭吃:
相关文章推荐
- 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)
- 在Android中自定义捕获Application全局异常,可以替换掉系统的强制退出对话框(很有参考价值与实用价值)
- Android 自定义CoordinatorLayout.Behavior 实现悬浮控件动画
- Android进阶——自定义View之扩展系统控件的另一种思路实现渐变文字动画的TextView
- android实现自定义RelativeLayout可拖动、缩放、旋转TextView
- 更新 --- android实现自定义RelativeLayout可拖动、缩放、旋转TextView
- 在Android中自定义捕获Application全局异常,可以替换掉系统的强制退出对话框(很有参考价值与实用价值)
- Android进阶——自定义View之组合系统控件实现水珠形状的ItemView
- 在Android中自定义捕获Application全局异常,可以替换掉系统的强制退出对话框
- Android进阶——自定义View之重写ViewGroup组合系统控件实现自定义ToolBar模板
- [Android教程] Android系统自定义实现日历控件
- Android进阶——自定义View之继承系统控件实现自带删除按钮动画效果和软键盘自动悬浮于文本框下方
- 在Android中自定义捕获Application全局异常,可以替换掉系统的强制退出对话框
- 自定义View控件详解(一) :Android LayoutInflater原理分析,带你一步步深入了解View
- Android控件——ListView之自定义Adapter补充LayoutInflater(其六)
- 在Android中自定义捕获Application全局异常,可以替换掉系统的强制退出对话框(很有参考价值与实用价值)
- 在Android中自定义捕获Application全局异常,可以替换掉系统的强制退出对话框
- 在Android中自定义捕获Application全局异常,可以替换掉系统的强制退出对话框(很有参考价值与实用价值)
- android自定义View实现裁剪图片功能,不使用系统的
- android 自定义发彩信实现(不调用系统发彩信界面)