在inflate时取到组件和获取属性的方法

  • Android里布局解析器的使用是采用工厂模式实现的,而 LayoutInflater.Factory2 正是那个工厂,我们所需做的就是继承并实现自定义的工厂实现特定功能,如下面的工厂类
  • 我的工厂功能是:在create方法中通过判断组件类型是不是textview,并获取TextView的text属性的id值。
  • 我的工厂用途是:配合多语言下发框架实现语言在线更新功能。

自定义工厂

public class CustomLanguageFactory implements LayoutInflater.Factory2 {
    private static CustomLanguageFactory instance;
    private static final String[] mPrefixes = {"android.widget.", "android.webkit.", "android.view."};
    private static final String mSchemas = "http://schemas.android.com/apk/res/android";
    private static final String mAttrText = "text";

    private CustomLanguageFactory() {
    }

    public static CustomLanguageFactory getInstance() {
        return instance != null ? instance : (instance = new CustomLanguageFactory());
    }

    public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
        return createView(name, context, attrs);
    }

    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return createView(name, context, attrs);
    }

    private View createView(String name, Context context, AttributeSet attrs) {
        View v = !name.contains(".") ? null : create(name, null, context, attrs);
        if (v == null) {
            for (String prefix : mPrefixes) {
                v = create(name, prefix, context, attrs);
                if (v != null) {
                    break;
                }
            }
        }
        return v;
    }

    private static View create(String name, String prefix, Context context, AttributeSet attrs) {
        try {
            View v = LayoutInflater.from(context).createView(name, prefix, attrs);
            if (v instanceof TextView) {
                String res = attrs.getAttributeValue(mSchemas, mAttrText);
                res = res.replace("@", "");
                if (res != null && res.length() > 0) {
                    int textId = Integer.parseInt(res);
                    ((TextView) v).setText(context.getText(textId));
                }
            }
            return v;
        } catch (Throwable e) {
            return null;
        }
    }


    public static void attach(Activity a) {
        attach(a.getLayoutInflater());
    }

    public static void attach(Dialog d) {
        attach(d.getLayoutInflater());
    }

    public static void attach(LayoutInflater li) {
        if (!(li.getFactory2() instanceof CustomLanguageFactory) && !(li.getFactory() instanceof CustomLanguageFactory)) {
            li.setFactory2(getInstance());
        }
    }
}

使用工厂

  • 通过实现BaseActivity,并让有需要的Activity继承
public class BaseActivity extends AppCompatActivity{

    @Override
    public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
        View v = super.onCreateView(parent, name, context, attrs);
        if (v == null) {
            CustomLanguageFactory.getInstance().onCreateView(parent, name, context, attrs);
        }
        return v;
    }

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        View v = super.onCreateView(name, context, attrs);
        if (v == null) {
            CustomLanguageFactory.getInstance().onCreateView(name, context, attrs);
        }
        return v;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getLayoutInflater().setFactory2(CustomLanguageFactory.getInstance());
        super.onCreate(savedInstanceState);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,035评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,194评论 4 61
  • 《豆蔻镇的居民和强盗》这本书今天终于读完了。 我想以前一样边读边划 读完后我立刻写了读后感 对于小镇上的这些人和事...
    欢乐的事儿阅读 1,508评论 0 0
  • 连续162天灵修经文 祭司乌利亚就照着亚哈斯王所吩咐的行了。 (列王纪下 16:16 和合本) 《感动》祭司的职责...
    报佳音阅读 2,885评论 0 0
  • 来自于外部的刺激使我感到活着并有活着的价值 美静,是我在心理工作坊上的同学。人如其名,美丽而安静,是很多人眼中羡慕...
    陌山心理阅读 2,833评论 0 0