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

时间:2014-01-09 16:59:04

标签: android attributes gradle android-custom-view

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

我的项目看起来像这样

- custom_icon_view //包含自定义属性的自定义视图的库 - 我的应用程序//这是实际使用自定义视图的主要应用程序

在我的布局中,我的命名空间如下所示:

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

IconView&gt; res&gt;值目录

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

        </resources>

5 个答案:

答案 0 :(得分:17)

无法真正看到您项目中出现的问题。这是我如何使用自定义视图&amp;我的人:

在我的图书馆项目中:

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"

by:

 custom:name="entypo_search"

答案 1 :(得分: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);

让它发挥作用

答案 2 :(得分:2)

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

  

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

答案 3 :(得分:1)

我认为你应该使用

iconview:name="entypo_search"

而不是

iconview:icon_name="entypo_search"

答案 4 :(得分:0)

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