【问题标题】:Custom elements and package name自定义元素和包名
【发布时间】:2012-05-13 10:00:38
【问题描述】:

在 Android 中创建具有属性的自定义元素时,我需要将应用程序的命名空间放入布局中。

例如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:whatever="http://schemas.android.com/apk/res/org.example.mypackage"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

    <org.example.mypackage.MyCustomView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:gravity="center"
      whatever:my_custom_attribute="Hello, world!" />
</LinearLayout>

这是否还要求我在 Eclipse 中的项目结构与清单中定义的 Android 包的名称相同 - 根据示例? 这也可以吗:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:whatever="http://schemas.android.com/apk/res/org.mycompany.myproduct"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

    <org.example.mypackage.MyCustomView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:gravity="center"
      whatever:my_custom_attribute="Hello, world!" />
</LinearLayout>

【问题讨论】:

    标签: android custom-attributes


    【解决方案1】:

    它应该反映您的应用程序的包名称。因此,按照您的方式使用它是正确的。

    例如:

    xmlns:whatever="http://schemas.android.com/apk/res/org.mycompany.myproduct 的最后一部分org.mycompany.myproduct 应该与您的包名 相同。您可以将xmlns:whatever 更改为xmlns:theblitz 之类的任何内容,但请确保您确实使用theblitz 作为xml 中属性的前缀。

    欲了解更多信息,请阅读this

    【讨论】:

    • 我认为您误解了我的意思(或者我误解了您的回答)。在第一个示例中,我在 xmlns 声明和客户元素的声明中都使用了“org.mycompany.myproduct”。在第二个中,自定义元素中的包是“org.example.mypackage”而不是“org.mycompany.myproduct”。
    • 视图类型org.example.mypackage.MyCustomView的定义方式与你的类文件所在的方式相同
    • 那么,这和 xmlns 中的值不必匹配 - 对吧?
    • xmlns 只需要反映包名即可。但视图类型应该反映完整的类路径。所以是的,两者都不能匹配为相同
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多