android - 是否可以为自定义视图创建建议?
解决方案
declare-styleable
可以在自定义视图的标签中使用枚举格式显示自定义选项的自定义选项。
首先,像这样在 attrs.xml 文件中声明所需的属性
<declare-styleable name="customView">
<attr name="customOption" format="enum">
<enum name="option1" value="0" />
<enum name="option2" value="1" />
</attr>
</declare-styleable>
customOption
现在像这样在您的 customView 中获取此值
const val OPTION_1 = 0 //for readability
const val OPTION_2 = 1 //for readability
var customOption = OPTION_1
init {
paint.isAntiAlias = true
setupAttributes(attrs)
}
private fun setupAttributes(attrs: AttributeSet?) {
val typedArray = context.theme.obtainStyledAttributes(attrs, R.styleable.customView, 0, 0)
customOption = typedArray.getInt(R.styleable.customView_customOption, OPTION_1.toInt())
}
推荐阅读
- google-apps-script - 查询语言参考:按日期请求
- swift - 如何根据推送通知的类型打开 Viewcontroller?
- amazon-web-services - 当我将 .war 文件推送到 s3 时,如何在从 s3 触发的 codedeploy 中自动部署?
- c# - 从在 C# 中的单独线程上运行的外部类更新 Windows Form Control (listview)
- php - 在 php 中为 Instagram api 设置代理
- python-3.x - 如何编写 Pytest 来检查命令行解释器生成的文件夹结构是否符合预期?
- ionic3 - 为什么可以在 iOS 上运行的应用程序不能在 android 上运行。?onic3 ?imagepicker 错误
- python - 从 CapsNet 中提取分类模块
- c++ - 铸造一个空指针来检查内存对齐
- azure-devops - 使用 Bitbucket 的 Azure DevOps yaml 配置始终返回“禁止”