我在我的 android 应用程序中为我的自定义视图使用自定义属性。我想为我的自定义属性提供一个浮点值。
资源/值/attrs.xml
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<declare-styleable name="CanvasView">
<attr name="backgroundColor" format="color"/>
<attr name="paintColor" format="color"/>
<attr name="paintStroke" format="float"/>
</declare-styleable>
</resources>
资源/布局/view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.example.canvaspoc.CanvasView
xmlns:customAttrs="http://schemas.android.com/apk/res/com.example.canvaspoc"
android:id="@+id/canvas"
android:layout_width="match_parent"
android:layout_height="match_parent"
customAttrs:backgroundColor="@android:color/holo_green_light"
customAttrs:paintColor="@android:color/secondary_text_light"
customAttrs:paintStroke="4f"/>
</RelativeLayout>
但是当我尝试运行应用程序时,它给了我以下错误
错误:(15, 34) 不允许使用字符串类型(在“paintStroke”处,值为“4f”)。
工作片段:
我们只需为自定义浮点属性值提供“4”或“4.0”。无需在结尾添加“f”。