在样式中使用App命名空间

Nou*_*vay 78 android android-layout android-theme android-styles

我将举一个例子来证明更重要的一点.

想象一下,我的应用程序有许多FloatingActionButtons.因此,我想创建一种样式并重用它.所以我做了以下事情:

<style name="FabStyle” parent ="Widget.Design.FloatingActionButton">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_margin">16dp</item>
    <item name="app:backgroundTint">@color/accent</item>
    <item name="app:layout_anchorGravity">end|bottom</item>
</style>
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是代码没有编译,因为它抱怨

Error:(40, 5) No resource found that matches the given name: attr 'app:backgroundTint'.
Run Code Online (Sandbox Code Playgroud)

我尝试通过resources标记引入命名空间,但这不起作用

<resources
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
Run Code Online (Sandbox Code Playgroud)

有什么想法我可以让它工作?

dex*_*dex 175

因为app你不需要指定app:<property name>.仅仅<property name>是不够的.

例如

<style name="FabStyle" parent="Widget.Design.FloatingActionButton"> 
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_margin">16dp</item>
    <item name="backgroundTint">@color/accent</item>
    <item name="layout_anchorGravity">end|bottom</item>
</style>
Run Code Online (Sandbox Code Playgroud)

并且layout_anchorGravity您需要在XML文件中将其设置为您定义浮动操作按钮.