android动态换肤系列2——android.view.LayoutInflater.Factory的使用

在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>

运行结果


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java.lang.IllegalStateException: ScrollView can host only one direct child at androidx.core.widget.NestedScrollView.addView(NestedScrollView.java:507) at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1131) at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72) at android.view.LayoutInflater.rInflate(LayoutInflater.java:1101) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088) at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1130) at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72) at android.view.LayoutInflater.rInflate(LayoutInflater.java:1101) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088) at android.view.LayoutInflater.inflate(LayoutInflater.java:686) at android.view.LayoutInflater.inflate(LayoutInflater.java:505) at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:360) at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:443) at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:121) at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:722) at com.android.tools.idea.rendering.RenderTask.lambda$inflate$9(RenderTask.java:879) at com.android.tools.idea.rendering.RenderExecutor$runAsyncActionWithTimeout$3.run(RenderExecutor.kt:194) at com.android.tools.idea.rendering.RenderExecutor$PriorityRunnable.run(RenderExecutor.kt:292) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) at java.base/java.lang.Thread.run(Thread.java:829)
最新发布
06-06

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值