切换到gradle后,android自定义视图属性不起作用 [英] android custom view attributes not working after switch to gradle
问题描述
所以我最近迁移到了gradle,现在我的自定义视图属性返回null
so I recently migrated to gradle now my custom view attributes return null
我的项目看起来像这样
-custom_icon_view//包含具有自定义属性的自定义视图的库 --my application//这是实际使用自定义视图的主应用程序
--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
在我的布局中,我的命名空间定义如下:
in my layout I have the namespace defined like this :
xmlns:iconview="http://schemas.android.com/lib/be.webelite.iconview"
因为使用apk/res-auto会重新调整一条错误,指出无法识别出服装
because using apk/res-auto retuns an error saying attibutes could not be identified
这是我尝试获取xml中定义的图标名称的方法,这曾经可以完美地工作 但现在没有.而我改变的只是迁移到gradle.
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.
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文件引起了问题吗?
so I'm guessing my gradle.build files are causing a problem?
我将库设置为
apply plugin: 'android-library'
以
apply plugin: 'android'
这已经让我头痛了2天了:(任何帮助/提示都非常贴心.
this has been giving me headache for 2 days now :( any help/hints are very apperciated.
这是我的gradle文件
here are my gradle files
http://pastie.org/private/h57o8nanydosq0dtm6eiq
这是文件夹结构
http://pastie.org/private/nvbzomx2zeagdpzt8qqjsq
这就是我在xml中声明视图的方式
this is how I declare my view in the 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> res>值目录中的
attrs.xml
attrs.xml in IconView>res>values directory
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="icon">
<attr name="name" format="string" />
</declare-styleable>
</resources>
推荐答案
无法真正看到项目中的问题.这是我使用自定义视图&的方法我的attrs:
Can't really see what's wrong in your project. Here is how I use custom view & attrs in mine :
在我的图书馆项目中:
attrs.xml:
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
}
在我的主项目中,我的一个布局示例:
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 ...
在我的主项目"的build.gradle中
in my build.gradle of my "main project"
dependencies {
compile project(":MyLibraryProject")
}
这是我的库的build.gradle:
And here the build.gradle of my library :
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
}
}
尝试使用此命名空间:
xmlns:custom="http://schemas.android.com/apk/res-auto"
并替换:
iconview:icon_name="entypo_search"
作者:
custom:name="entypo_search"
这篇关于切换到gradle后,android自定义视图属性不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!