如何访问 Android 的图书馆资源?

How can I access to the resources of a library for Android?

我正在尝试访问包含一些字符串和可绘制对象的库。在我的主项目中,我将库添加为一个模块。问题是,当我想在布局中引用字符串或颜色资源时,我做不到。 根据 API 语法的指南,我使用 @[package.of.the.library]:type/resource_name,但这不起作用。

我添加了一个用于测试的布局示例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mylibrary="http://schemas.android.com/apk/com.mylibrary.resources"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@mylibrary:string/testing_text" />
</LinearLayout>

我试过使用 xmlns:mylibrary="http://schemas.android.com/apk/res-auto" 而不是 xmlns:mylibrary="http://schemas.android.com/apk/com.mylibrary.resources" 但它不起作用。

首先将模块作为依赖项添加到主项目build.gradle文件

compile project(":module_name")

其中 module_name 是模块项目的名称

然后您可以像访问普通字符串和颜色一样访问字符串或颜色

@string/testing_text

您需要将 compile project(":mylibrary") 添加到您的应用 build.gradle 文件依赖项。

dependencies {
    compile project(":mylibrary")
    ...rest of dependencies for project
}