在 XML 中使用自定义视图而不使用完全限定的类名 [英] Using custom Views in XML without using fully-qualified class name

查看:23
本文介绍了在 XML 中使用自定义视图而不使用完全限定的类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于定义为主题的按钮,我有自己的样式,但我也使用自己的类来处理按钮(因为有自己的字体).是否可以用一个漂亮的名字来调用我的按钮,例如

I have my own style for buttons defined as themes but I also use my own class to handle buttons (because of own fonts). Is it possible to call my button with a pretty name such as

<MyButton>

代替

<com.wehavelongdomainname.android.ui.MyButton>

推荐答案

令人惊讶的是,答案是是".我最近了解了这一点,这实际上是您可以做的一些事情来使您的自定义视图膨胀更有效.IntelliJ 仍然警告您它无效(尽管它会成功编译和运行)——我不确定 Eclipse 是否会警告您.

So the answer, surprisingly, is "yes". I learned about this recently, and it's actually something you can do to make your custom view inflation more efficient. IntelliJ still warns you that its invalid (although it will compile and run successfully) -- I'm not sure whether Eclipse warns you or not.

无论如何,您需要做的是定义您自己的 LayoutInflater.Factory 子类:

Anyway, so what you'll need to do is define your own subclass of LayoutInflater.Factory:

public class CustomViewFactory implements LayoutInflater.Factory {
    private static CustomViewFactory mInstance;

    public static CustomViewFactory getInstance () {
        if (mInstance == null) {
            mInstance = new CustomViewFactory();
        }

        return mInstance;
    }

    private CustomViewFactory () {}

    @Override
    public View onCreateView (String name, Context context, AttributeSet attrs) {
        //Check if it's one of our custom classes, if so, return one using
        //the Context/AttributeSet constructor
        if (MyCustomView.class.getSimpleName().equals(name)) {
            return new MyCustomView(context, attrs);
        }

        //Not one of ours; let the system handle it
        return null;
    }
}

然后,在您为包含这些自定义视图的布局膨胀的任何活动或上下文中,您都需要将工厂分配给该上下文的 LayoutInflater:

Then, in whatever activity or context in which you're inflating a layout that contains these custom views, you'll need to assign your factory to the LayoutInflater for that context:

public class CustomViewActivity extends Activity {
    public void onCreate (Bundle savedInstanceState) {
        //Get the LayoutInflater for this Activity context
        //and set the Factory to be our custom view factory
        LayoutInflater.from(this).setFactory(CustomViewFactory.getInstance());

        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_with_custom_view);
    }
}

然后您可以在 XML 中使用简单的类名:

You can then use the simple class name in your XML:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:orientation="vertical"
             android:layout_width="match_parent"
             android:layout_height="match_parent">

    <MyCustomView
        android:id="@+id/my_view"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_gravity="center_vertical" />

</FrameLayout>

这篇关于在 XML 中使用自定义视图而不使用完全限定的类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆