min*_*haz 5 xml user-interface android themes
所以,在Android上我们有类似的属性colorPrimaryDark可以通过以下方式访问:
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
app:elevation="6dp"
app:layout_anchor="@id/recycler_view"
android:layout_marginRight="40dp"
android:layout_marginBottom="86dp"
app:fabSize="normal"
app:backgroundTint="?android:attr/colorPrimaryDark"
app:layout_anchorGravity="bottom|right|end" />
Run Code Online (Sandbox Code Playgroud)
现在,我决定创建自己的属性.所以,这就是我所做的;
第1步在attrs.xml中
<?xml version="1.0" encoding="utf-8"?>
<resources >
<declare-styleable name="AppTheme">
<attr name="colorLeadingDark" format="color" />
</declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)
Styles.xml中的第2步
<style name="AppTheme">
<item name="colorLeadingDark">#9C27B0</item>
</style>
Run Code Online (Sandbox Code Playgroud)
第3步工厂
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
app:elevation="6dp"
app:layout_anchor="@id/recycler_view"
android:layout_marginRight="40dp"
android:layout_marginBottom="86dp"
app:fabSize="normal"
app:backgroundTint="?attr/colorLeadingDark"
app:layout_anchorGravity="bottom|right|end" />
Run Code Online (Sandbox Code Playgroud)
第4步设置主题
我onCreate在我的活动中设置了这个主题
当我运行我的代码时,这没问题.我可以看到我想要的FAB颜色.但是,问题是当我点击FAB时我崩溃了.
android.view.InflateException: Binary XML file line #17: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:640)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
Run Code Online (Sandbox Code Playgroud)
第17行是XML声明中FAB的第一行.
以下情况很好:
colorPrimaryDark它是系统定义(这是告诉我,我的代码是罚款)TypedArray typedArray=context.obtainStyledAttributes(getCurrentTheme(), attrs);.(这告诉我我的自定义属性实现是正确的.)现在问题是我点击的时候.我究竟做错了什么?