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

本文介绍如何通过自定义LayoutInflater.Factory来干预Android中View的创建过程,实现特定需求,如动态改变View属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

运行结果


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值