【问题标题】:android custom view attributes not working after switch to gradle切换到gradle后android自定义视图属性不起作用
【发布时间】:2014-01-28 09:00:50
【问题描述】:

所以我最近迁移到 gradle 现在我的自定义视图属性返回 null

我的项目是这样的

--custom_icon_view // 保存具有自定义属性的自定义视图的库 --my application // 这是实际使用自定义视图的主应用程序

在我的布局中,我有这样定义的命名空间:

        xmlns:iconview="http://schemas.android.com/lib/be.webelite.iconview"

因为使用 apk/res-auto 会返回一个错误,提示无法识别属性

这就是我尝试获取在 xml 中定义的图标名称的方式,这曾经可以完美地工作 但现在它没有。而我所做的只是迁移到 gradle。

        final TypedArray a              = context.obtainStyledAttributes(attrs,be.webelite.iconview.R.styleable.icon);
        icon_name = a.getString(be.webelite.iconview.R.styleable.icon_name);

所以我猜我的 gradle.build 文件导致了问题?

我将库设置为

  apply plugin: 'android-library'

将主应用程序 gradle.build 结束为

  apply plugin: 'android'

这已经让我头疼 2 天了 :( 非常感谢任何帮助/提示。

这是我的 gradle 文件

http://pastie.org/private/h57o8nanydosq0dtm6eiq

这是文件夹结构

http://pastie.org/private/nvbzomx2zeagdpzt8qqjsq

这就是我在 xml 中声明我的视图的方式

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    <!-- tried res-auto didn't work -->
    xmlns:iconview="http://schemas.android.com/apk/lib/be.webelite.iconview"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background_gray">

    <be.webelite.iconview.IconView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        iconview:icon_name="entypo_search"
        android:textSize="25dp"/>

图标视图中的attrs.xml>res>values目录

        <?xml version="1.0" encoding="utf-8"?>
        <resources>
            <declare-styleable name="icon">
                <attr name="name" format="string" />
            </declare-styleable>

        </resources>

【问题讨论】:

标签: android attributes gradle android-custom-view


【解决方案1】:

看不出你的项目出了什么问题。这是我在我的中使用自定义视图和属性的方式:

在我的图书馆项目中:

attrs.xml:

<declare-styleable name="CustomFontSize">
    <attr name="typeFace" format="string" />
</declare-styleable>

在我的自定义类中:

 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomFontSize);
 if (a == null) {
      return;
 }
 CharSequence s = a.getString(R.styleable.CustomFontSize_typeFace);
 if (s != null) {
    // do something
 }

在我的主项目中,这里是我的布局之一的示例:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:custom="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              android:paddingLeft="@dimen/border_margin_pulltorefresh"
              android:paddingRight="@dimen/border_margin_pulltorefresh"
              android:paddingBottom="@dimen/divider_height">


    <com.custom.view.TextViewFont
            style="@style/DateStyle"
            android:id="@+id/news_date"
            android:shadowColor="@color/white"
            android:layout_gravity="center_vertical"
            android:gravity="center_vertical"
            custom:typeFace="@string/font_roboto_condensed_bold"/>

</LinearLayout>

希望对你有所帮助...

编辑:

在我的“主项目”的 build.gradle 中

dependencies {
    compile project(":MyLibraryProject")
}

这里是我的库的 build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.7.+'
    }
}
apply plugin: 'android-library'

repositories {
    mavenCentral()
}

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile 'com.android.support:support-v4:19.0.0'
    compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
    compile project(':WebediaCore:dependencies:DataDroid')
    compile project(':WebediaCore:dependencies:ViewPagerIndicator')
    compile project(':WebediaCore:dependencies:ActionBar-PullToRefresh')
    compile project(':WebediaCore:dependencies:Android-Universal-Image-Loader')
}

android {
    compileSdkVersion 19
    buildToolsVersion '19'

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 19
    }
}

EDIT1:

尝试使用这个命名空间:

xmlns:custom="http://schemas.android.com/apk/res-auto"

并替换:

 iconview:icon_name="entypo_search"

作者:

 custom:name="entypo_search"

【讨论】:

  • 谢谢,但是您使用的是 grub 吗?如何在项目中声明库模块?
  • 当我为我的库使用与您完全相同的 gradle.build 时,我收到一条错误消息,提示“缺少主清单”...
  • 因为我使用的是默认的gradle项目结构,所以不需要在build.gradle中声明“sourceSets”。你可以试一试,或者把你的 sourceSet 放在 build.gradle 中。 gradle 的默认项目结构如下所述:tools.android.com/tech-docs/new-build-system/…
  • 当我使用 res-auto 时,我收到此错误“在包 'com.app.myapp' 中找不到属性 'icon_name' 的资源标识符”,但 iconview 具有不同的包名称“be.webelite .iconview" 你的库有相同的包名吗?
  • 是的,我的库有不同的包名。请尝试我上次的编辑。
【解决方案2】:

如果您在自定义视图的 init 方法中使用对该特定命名空间的硬引用来获取属性,那么更改为 res-auto 将破坏该属性。相反,您还需要将这些引用更改为 res-auto。或者首选的方法是只获取类型化的接口。例如,在我的项目中:

textStyle = attrs.getAttributeIntValue("http://schemas.android.com/apk/res/com.bodybuilding.mobile", "textStyle", 10);
shadowInner = attrs.getAttributeBooleanValue("http://schemas.android.com/apk/res/com.bodybuilding.mobile", "shadowInner", true);
shadowOuter = attrs.getAttributeBooleanValue("http://schemas.android.com/apk/res/com.bodybuilding.mobile", "shadowOuter", false);
allowResize = attrs.getAttributeBooleanValue("http://schemas.android.com/apk/res/com.bodybuilding.mobile", "allowResize", true);

变成了这个:

textStyle = attrs.getAttributeIntValue("http://schemas.android.com/apk/res-auto", "textStyle", 10);
shadowInner = attrs.getAttributeBooleanValue("http://schemas.android.com/apk/res-auto", "shadowInner", true);
shadowOuter = attrs.getAttributeBooleanValue("http://schemas.android.com/apk/res-auto", "shadowOuter", false);
allowResize = attrs.getAttributeBooleanValue("http://schemas.android.com/apk/res-auto", "allowResize", true);

但最好是这样:

TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.BBcomButton);
textStyle = a.getInt(R.styleable.BBcomButton_textStyle, 10);
shadowInner = a.getBoolean(R.styleable.BBcomButton_shadowInner, true);
shadowOuter = a.getBoolean(R.styleable.BBcomButton_shadowOuter, false);
shadowInnerColor = a.getColor(R.styleable.BBcomButton_shadowInnerColor, 0xff000000);
shadowOuterColor = a.getColor(R.styleable.BBcomButton_shadowOuterColor, 0xff000000);
allowResize = a.getBoolean(R.styleable.BBcomButton_allowResize, true);

让它发挥作用

【讨论】:

    【解决方案3】:

    我认为你应该使用

    iconview:name="entypo_search"
    

    而不是

    iconview:icon_name="entypo_search"
    

    【讨论】:

    • @mars 使用这个 xmlns:iconview="schemas.android.com/apk/res/be.webelite.iconview"
    • still null :s 我觉得奇怪的是相同的代码适用于旧的构建方法,但不适用于 gradle? res-auto 在没有 gradle 的情况下完美运行:s
    • @mars 真的连线了。但我使用 gradle 和 res-auto 很好。
    【解决方案4】:

    当您将包名静态声明为命名空间(例如 xmlns:iconview="http://schemas.android.com/apk/lib/be.webelite.iconview" in您的情况) 在布局文件中并尝试使用 Lint 构建项目。检查Android Gradle Lint checks

    在 Gradle 项目中,最终 APK 中使用的实际包可能会有所不同;为了 例如,您可以在一个版本而不是另一个版本中添加 .debug 包后缀。 因此,您不应该在资源中硬编码应用程序包; 相反,使用特殊的命名空间http://schemas.android.com/apk/res-auto 这将导致工具为资源找出正确的命名空间 无论构建期间使用的实际包是什么。

    【讨论】:

      【解决方案5】:

      如果有人在这里,但似乎没有任何工作。据我所知,在您的命名空间中添加下划线在编辑器中似乎可以正常工作,但在您构建应用程序时会意外中断。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-05-18
        • 1970-01-01
        • 1970-01-01
        • 2020-01-18
        • 1970-01-01
        • 1970-01-01
        • 2013-09-01
        相关资源
        最近更新 更多