android - 在android studio中找不到我的自定义视图的自定义属性
问题描述
我正在尝试在 android studio 中为我的自定义视图声明自定义视图。但 android studio 不断向我显示这个错误
AAPT: error: attribute bubbleSize (aka com.first.myapplication:bubbleSize) not found.
这是我的代码:
我的自定义视图,只是声明:
package com.first.myapplication
import android.content.Context
import android.view.View
class Bubble(context: Context): View(context) {
}
attr.xml 文件,我在其中定义我的自定义属性:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<declare-styleable name="Bubble">
<attr name="bubbleSize" format="enum">
<enum name="small" value="10"/>
<enum name="big" value="25"/>
</attr>
</declare-styleable>
</resources>
我使用自定义视图和自定义属性的 activity_main 布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.first.myapplication.Bubble
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:bubbleSize="small"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
解决方案
请将您的attr.xml
文件从xml
文件夹移动到res->values
文件夹并更改XML
如下:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<declare-styleable name="Bubble">
<attr name="bubbleSize" format="enum">
<enum name="small" value="0"/>
<enum name="big" value="1"/>
</attr>
</declare-styleable>
</resources>
推荐阅读
- android - 创建项目后:资源编译失败
- linux - Freeradius 问题读取 google_authenticator 机密 Centos 7
- c++ - QComboBox 或 QListWidget 中选定元素的样式文本?
- angular - 如何从父组件输入的对象中获取数据。
- python-3.x - Windows 10 中的 anaconda 环境中缺少 bin 目录
- visual-studio-2017 - 文件未出现在解决方案资源管理器中
- excel - Excel for mac 表单按钮未运行宏
- r - 标题的 R Markdown 索引
- android - Flutter 和 Firebase 谷歌登录
- qt - 在 QPushButton 中放置一个图标