【问题标题】:Android XML Attrs Not working with Custom LibraryAndroid XML Attrs 不适用于自定义库
【发布时间】:2016-06-29 02:24:41
【问题描述】:

我目前正在构建一个库项目,但我遇到了一些关于 XML 属性的问题。在库模块中,我创建了declare-styleable in attrs.xml

<resources>
<declare-styleable name="betterTextViewAttrs">
    <attr name="typefaceAsset" format="string"/>
    <attr name="scaleWithinBounds" format="boolean"/>
</declare-styleable>

我想在项目的示例布局中引用 attrs。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout     xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:bettertv="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.virtualprodigy.bettertextviewsample.MainActivity">

<com.virtualprodigy.bettertextview.BetterTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    bettertv:typefaceAsset="dancing _script.ttf"
    bettertv:scaleWithinBounds="false"
    app:layout_constraintBottom_toBottomOf="@+id/activity_main"
    app:layout_constraintLeft_toLeftOf="@+id/activity_main"
    app:layout_constraintRight_toRightOf="@+id/activity_main"
    app:layout_constraintTop_toTopOf="@+id/activity_main" />

但由于某种原因,xmlns:bettertv="http://schemas.android.com/apk/res-auto" 无法正常工作。编译时出现以下错误。

Error:(11) No resource identifier found for attribute 'typefaceAsset' in package 'com.virtualprodigy.bettertextviewsample'

可以在 GitHub 上找到完整的源代码。任何帮助表示赞赏。

BetterTextView GitHub Project

【问题讨论】:

    标签: android xml xml-namespaces android-library


    【解决方案1】:

    盯着我的项目后,我意识到了这个问题。即使 Android Studio 通过 GUI 为您创建了一个库项目,它也不会自动在 build.gradle 文件中添加 compile project(':bettertextview')。更新这个完全解决了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-28
      • 1970-01-01
      • 2021-07-16
      相关资源
      最近更新 更多