【问题标题】:How can I access to the resources of a library for Android?如何访问 Android 库的资源?
【发布时间】:2016-05-11 17:32:36
【问题描述】:

我正在尝试访问包含一些字符串和可绘制对象的库。在我的主项目中,我将库添加为模块。问题是当我想在布局中引用字符串或颜色资源时,我不能。 根据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",但它不起作用。

【问题讨论】:

    标签: android android-resources android-library


    【解决方案1】:

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

    compile project(":module_name")
    

    其中 module_name 是模块项目的名称

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

    @string/testing_text
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 你看到我的回答了吗?我提出了同样的建议
      • @ankitagrawal 是的,这是真的,但我通过代码的贡献标记了 Joopkins 的答案。我也给你的答案投票了,谢谢!
      • 好的,我不知道您需要代码,感谢您的解释,如果 joopkins 回答可以更好地帮助您,没有问题,无论如何使用我的第二个建议来访问资源和字符串
      猜你喜欢
      • 2014-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-22
      • 1970-01-01
      • 1970-01-01
      • 2018-06-27
      • 1970-01-01
      相关资源
      最近更新 更多