android动态换肤系列2——android.view.LayoutInflater.Factory的使用
2016-02-29 15:30
786 查看
在Activity onCreate 的时候,setContentView(int layoutResID)方法引起的View创建会由android.app.Activity.getLayoutInflater().setFactory(mFactory)的mFactory完成。
如果没有用setFactory去设置,则采用默认工厂。
通过自定义mFactory然后setFactory设置进去可以在创建View时进行自定义干预。
FactoryActivity.java文件
package com.thinking.skintest;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.LayoutInflater.Factory;
import android.view.View;
import android.widget.TextView;
public class FactoryActivity extends Activity {
private ThinkingFactory mFactory;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mFactory = new ThinkingFactory();
getLayoutInflater().setFactory(mFactory);
// 将布局文件set进来之后,所有的View创建均由mFactory完成
setContentView(R.layout.activity_factory);
}
}
class ThinkingFactory implements Factory {
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
String str = attrs.getAttributeValue(
"http://schemas.android.com/thinking", "test_str");
// 创建View-----------------------start
View view = null;
try {
if (-1 == name.indexOf('.')) {
if ("View".equals(name)) {
view = LayoutInflater.from(context).createView(name,
"android.view.", attrs);
}
if (view == null) {
view = LayoutInflater.from(context).createView(name,
"android.widget.", attrs);
}
if (view == null) {
view = LayoutInflater.from(context).createView(name,
"android.webkit.", attrs);
}
} else {
view = LayoutInflater.from(context).createView(name, null,
attrs);
}
} catch (Exception e) {
e.printStackTrace();
view = null;
}
// 创建View-----------------------end
if (view == null) {
return null;
}
// 拿到View之后进行自定义处理---------start
if (view instanceof TextView) {
TextView t_view = (TextView) view;
t_view.setText(str + "_" + t_view.getText().toString());
}
// 拿到View之后进行自定义处理---------end
return view;
}
}
MyText.java文件
package com.thinking.skintest;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
public class MyText extends TextView {
// 这两个函数是自定义View必须实现的
public MyText(Context context) {
super(context);
}
public MyText(Context context, AttributeSet attrs) {
super(context, attrs);
String str = attrs.getAttributeValue(
"http://schemas.android.com/thinking", "test_str");
this.setText(str + "_" + this.getText());
}
}
activity_factory.xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:thinking="http://schemas.android.com/thinking"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.thinking.skintest.MyText
android:id="@+id/txt_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="aaaa"
thinking:test_str="yuyong" />
</RelativeLayout>
运行结果
如果没有用setFactory去设置,则采用默认工厂。
通过自定义mFactory然后setFactory设置进去可以在创建View时进行自定义干预。
FactoryActivity.java文件
package com.thinking.skintest;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.LayoutInflater.Factory;
import android.view.View;
import android.widget.TextView;
public class FactoryActivity extends Activity {
private ThinkingFactory mFactory;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mFactory = new ThinkingFactory();
getLayoutInflater().setFactory(mFactory);
// 将布局文件set进来之后,所有的View创建均由mFactory完成
setContentView(R.layout.activity_factory);
}
}
class ThinkingFactory implements Factory {
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
String str = attrs.getAttributeValue(
"http://schemas.android.com/thinking", "test_str");
// 创建View-----------------------start
View view = null;
try {
if (-1 == name.indexOf('.')) {
if ("View".equals(name)) {
view = LayoutInflater.from(context).createView(name,
"android.view.", attrs);
}
if (view == null) {
view = LayoutInflater.from(context).createView(name,
"android.widget.", attrs);
}
if (view == null) {
view = LayoutInflater.from(context).createView(name,
"android.webkit.", attrs);
}
} else {
view = LayoutInflater.from(context).createView(name, null,
attrs);
}
} catch (Exception e) {
e.printStackTrace();
view = null;
}
// 创建View-----------------------end
if (view == null) {
return null;
}
// 拿到View之后进行自定义处理---------start
if (view instanceof TextView) {
TextView t_view = (TextView) view;
t_view.setText(str + "_" + t_view.getText().toString());
}
// 拿到View之后进行自定义处理---------end
return view;
}
}
MyText.java文件
package com.thinking.skintest;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
public class MyText extends TextView {
// 这两个函数是自定义View必须实现的
public MyText(Context context) {
super(context);
}
public MyText(Context context, AttributeSet attrs) {
super(context, attrs);
String str = attrs.getAttributeValue(
"http://schemas.android.com/thinking", "test_str");
this.setText(str + "_" + this.getText());
}
}
activity_factory.xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:thinking="http://schemas.android.com/thinking"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.thinking.skintest.MyText
android:id="@+id/txt_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="aaaa"
thinking:test_str="yuyong" />
</RelativeLayout>
运行结果
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories