是否可以缩短自定义视图名称? [英] Is it possible to shorten custom view names?

查看:38
本文介绍了是否可以缩短自定义视图名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我创建了一个名为 MyView 的自定义视图,并希望在 xml 中使用它,并且该视图位于 com.example 包中.我需要做这样的事情:

Let's say that I created a custom view called MyView and want to use it in the xml and the view is located in com.example package. I need to do something like this:

<com.example.MyView
   ......
/>

每次使用视图时,我都需要写下包名.如果我的包名很长怎么办?

I need to write the package name every time I use the view. What if I have a long package name?

<what.a.long.long.long.loooooong.packagename.MyView
   ......
/>

这看起来很丑陋.是否可以缩短包名?我需要在 AndroidManifest.xml 文件中做些什么吗?

That just looks ugly. Is it possible to shorten the package name? Do I need to do something in the AndroidManifest.xml file?

推荐答案

定义你自己的命名空间:

Define your own namespace:

<Layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:x="what.a.long.long.long.loooooong.packagename."
    >
    <x:MyView>
    </x:MyView>
</Layout>

xmlns:x 指定所有以 <x: 开头的标签都应该在命名空间定义(what.a.long.long.long.looooooong.packagename. 在这种情况下).

xmlns:x spefifies that all tags that start with <x: should be looked up in package that is defined in the namespace definition (what.a.long.long.long.loooooong.packagename. in this case).

您甚至可以在标签本身中使用命名空间:

You can even use the namespace within the tag itself:

<x:MyView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:x="what.a.long.long.long.loooooong.packagename."
    />

这篇关于是否可以缩短自定义视图名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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