【问题标题】:Android API with custom View IDs具有自定义视图 ID 的 Android API
【发布时间】:2014-08-20 02:42:36
【问题描述】:

对于内置 Android ListView 之类的东西,您可以通过 android:id="@android:id/text1" 引用 XML 中的 ID

假设我有一个名为 MyDomain 的外部库,其中有一些有用的布局 API。

在我的主应用程序中,我想做类似android:id="@mydomain:id/someTextId" 的操作。

我怎样才能做到这一点?

我正在使用 Android Studio 版本 0.8.6 测试版,并且(暂时)将库作为模块,如果它有任何不同的话

【问题讨论】:

  • 这不容易。

标签: android xml


【解决方案1】:

这篇文章How to reference a string from another package in a library using XML in Android? 中有一个很好的解释,解释了那里“包”的含义以及为什么你不能像这样使用它,除非你使用共享库

【讨论】:

    【解决方案2】:

    我不确定你到底想做什么......

    您可以与您的应用共享库的组件。应用程序的布局将是这样的:

    <com.my.domain.MyComponent
      xmlns:android="http://schemas.android.com/apk/res/android"
      ...
    />
    

    您还可以通过添加 XML 命名空间直接在布局中定义组件的自定义属性,例如:

    xmlns:md="http://schemas.android.com/apk/res/com.my.domain.MyComponent"
    

    然后你可以使用像md:my_property="42" 这样的属性。

    同时检查这个答案:How can I set up custom namespaces in layout file in Android studio?

    【讨论】:

    • 谢谢,但我不想要自定义属性。我只想为要使用的资源文件指定我自己的 ID。例如,在 XML 中使用 ListView 时,可以使用 android:id="@android:id/list"。我仍然想使用android:id,但我希望能够允许使用我自己的外部包中的ID,例如android:id="@mylib:id/headerView"
    猜你喜欢
    • 2013-09-01
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 2012-06-27
    • 2011-11-23
    • 2011-05-03
    • 1970-01-01
    相关资源
    最近更新 更多