切换到gradle后android自定义视图属性不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21026518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:51:43  来源:igfitidea点击:

android custom view attributes not working after switch to gradle

androidattributesgradleandroid-custom-view

提问by Mars

so I recently migrated to gradle now my custom view attributes return null

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

my project looks like this

我的项目看起来像这样

--custom_icon_view // library that holds the custom view with custom attributes --my application // this is the main app that actually uses the custom view

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

in my layout I have the namespace defined like this :

在我的布局中,我的命名空间定义如下:

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

because using apk/res-auto retuns an error saying attibutes could not be identified

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

this is how I try to get the icon name defined in xml, this used to work perfectlly but now it doesnt. and all I changed was migrating to gradle.

这就是我尝试获取在 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);

so I'm guessing my gradle.build files are causing a problem?

所以我猜我的 gradle.build 文件引起了问题?

I have the library set to

我将图书馆设置为

  apply plugin: 'android-library'

end the main app gradle.build as

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

  apply plugin: 'android'

this has been giving me headache for 2 days now :( any help/hints are very apperciated.

这已经让我头痛了 2 天了 :( 任何帮助/提示都非常有用。

here are my gradle files

这是我的 gradle 文件

http://pastie.org/private/h57o8nanydosq0dtm6eiq

http://pastie.org/private/h57o8nanydosq0dtm6eiq

and here is the folder structure

这是文件夹结构

http://pastie.org/private/nvbzomx2zeagdpzt8qqjsq

http://pastie.org/private/nvbzomx2zeagdpzt8qqjsq

this is how I declare my view in the xml

这就是我在 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 in IconView>res>values directory

IconView>res>values 目录中的 attrs.xml

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

        </resources>

回答by Andros

Can't really see what's wrong in your project. Here is how I use custom view & attrs in mine :

无法真正看出您的项目出了什么问题。这是我在我的中使用自定义视图和属性的方法:

In my library project :

在我的图书馆项目中:

attrs.xml :

attrs.xml :

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

in my custom class :

在我的自定义类中:

 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
 }

In my Main Project here an example of one of my layout :

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

<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>

Hope it will help ...

希望它会有所帮助...

Edit :

编辑 :

in my build.gradle of my "main project"

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

dependencies {
    compile project(":MyLibraryProject")
}

And here the build.gradle of my library :

这里是我图书馆的 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 :

编辑1:

Try to use this namespace :

尝试使用这个命名空间:

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

and replace :

并替换:

 iconview:icon_name="entypo_search"

by :

经过 :

 custom:name="entypo_search"

回答by GeliteNight

I think you should use

我认为你应该使用

iconview:name="entypo_search"

instead of

代替

iconview:icon_name="entypo_search"

回答by alphonzo79

If you are grabbing attributes in your custom view's init method with hard references to that specific namespace then changing to res-auto will break that. Instead, you will need to change those references also to res-auto. Or the preferred method is to just grab the typed interface. For example, in my project this:

如果您在自定义视图的 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);

Became either this:

变成了这样:

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);

but preferably this:

但最好是这样:

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);

To make it work

让它发挥作用

回答by Jason Rogena

You will get this error when you have statically declared your package name as a namespace (e.g xmlns:iconview="http://schemas.android.com/apk/lib/be.webelite.iconview" in your case)in a layout file and try building the project with Lint on. Check the Android Gradle Lint checks:

当您在布局中将包名静态声明为命名空间(例如 xmlns:iconview="http://schemas.android.com/apk/lib/be.webelite.iconview" 在您的情况下)时,您将收到此错误文件并尝试在打开 Lint 的情况下构建项目。检查Android Gradle Lint 检查

In Gradle projects, the actual package used in the final APK can vary; for example,you can add a .debug package suffix in one version and not the other. Therefore, you should not hardcode the application package in the resource; instead, use the special namespace http://schemas.android.com/apk/res-autowhich will cause the tools to figure out the right namespace for the resource regardless of the actual package used during the build.

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

回答by JamEngulfer

If anyone is here and nothing appears to be working. From what I can see, having an underscore in your namespace appears to work fine in the editor, but breaks unexpectedly as you build the app.

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