【问题标题】:How to inherit style from library style?如何从库样式继承样式?
【发布时间】:2017-04-03 20:05:42
【问题描述】:

我想在我的项目中使用NumberProgressBar 库。

我是这样在gradle中添加的:

dependencies {
   compile 'com.daimajia.numberprogressbar:library:1.2@aar'
}

它有预定义的样式,据说你可以这样轻松地使用它:

 <com.daimajia.numberprogressbar.NumberProgressBar
        android:id="@+id/number_progress_bar"
        style="@style/NumberProgressBar_Default"
  />

我想根据库样式编写自己的样式,但我无法从我的 styles.xml 文件中访问它:

<style name="CustomProgressBar" parent="NumberProgressBar_Beauty_Red"/>

Error:(2220) Error retrieving parent for item: No resource found that matches the given name 'NumberProgressBar_Beauty_Red'.

我尝试了几种不同的方法来访问它,使用命名空间,但它仍然不起作用。

甚至可以从库中继承样式吗?访问库样式的正确方法是什么?

【问题讨论】:

  • 您找到解决方案了吗?我有同样的问题
  • 不幸的是没有:(我不知道为什么这在我的情况下不起作用

标签: android android-layout inheritance android-styles android-library


【解决方案1】:

您确定以正确的方式添加依赖项并执行 Gradle Sync?

如果不是,请按照以下步骤操作:

  1. 文件 -> 项目结构...
  2. 选择左侧的app模块,然后选择Dependencies选项卡。
  3. 使用右侧的绿色+ 按钮并选择库依赖关系
  4. 粘贴com.daimajia.numberprogressbar:library:1.2@aar
  5. 好的好的

如果你想比较,这里是我的 build.gradle(Module:app)

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    testCompile 'junit:junit:4.12'
    compile 'com.daimajia.numberprogressbar:library:1.2@aar'
}

【讨论】:

  • 是的,我确定。我可以使用此布局,但无法在任何地方访问他的样式。
  • 我不确定我是否理解你。您正在尝试在 res->**values**->**styles.xml** 中创建新样式?
  • 是的。我想在我的 res/values/styles.xml 中创建新样式,其中父样式是库 styles.xml 中定义的样式
  • 奇怪,因为我刚刚用你的代码做到了,没有任何错误。尝试从依赖项(build.gradle(Module:app))中删除它,同步并再次添加,但自动(使用上述步骤),而不是手动(直接在 gradle 中添加代码)。需要明确的是,我并不是告诉你要避免手动操作,因为我认为你不知道该怎么做,而是因为可能会出现一些错误。
  • 还是不行。也许重要的是,当我使用 NumberProgressBar 进入布局的设计选项卡时,我遇到了渲染问题:Rendering Problems The following classes could not be instantiated: - com.daimajia.numberprogressbar.NumberProgressBar (Open Class, Show Exception, Clear Cache) Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE.
猜你喜欢
  • 1970-01-01
  • 2017-05-20
  • 2014-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多