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

标签 android attributes gradle android-custom-view

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

我的项目是这样的

--custom_icon_view//保存具有自定义属性的自定义 View 的库 --my application//这是实际使用自定义 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 as

  apply plugin: 'android'

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

这是我的gradle文件

http://pastie.org/private/h57o8nanydosq0dtm6eiq

这是文件夹结构

http://pastie.org/private/nvbzomx2zeagdpzt8qqjsq

这是我在 xml 中声明我的 View 的方式

<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>res>values 目录下的attrs.xml

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

        </resources>

最佳答案

无法真正看出您的项目有什么问题。以下是我如何在我的中使用自定义 View 和属性:

在我的图书馆项目中:

属性.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
    }
}

编辑1:

尝试使用这个命名空间:

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

并替换:

 iconview:icon_name="entypo_search"

作者:

 custom:name="entypo_search"

关于切换到 gradle 后,android 自定义 View 属性不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21026518/

相关文章:

jQuery - 属性与属性

php - 获取产品自定义属性以在 WooCommerce 产品循环中显示它们

maven - 如何在具有相互依赖关系的多模块 Android Gradle 项目中设置依赖关系?

java - 如何使用 Serenity 调用 IEDriver

python - 不是有效 python 标识符的属性

android - React Native - 从 package.json 到 android 构建 list 的自动版本名称

android - 运行ADB时出错:未找到Android设备-EXPO-CLI Linux

android - 在 gradle 命令中添加变量

java - Android 导入重叠

android - RelativeLayout,无法解析 id