android - Android,重用不是布局,而是通过 XML 重用视图
问题描述
我有一个在我的布局中经常使用的按钮。或者更确切地说,我有十个按钮,它们都具有相同的文本颜色、背景颜色、文本大小、宽度和高度。我不想为每个按钮定义所有这些参数。所以我希望能够写出类似...
<include view="@layout/standard_button"
android:id="@+id/button-id"
android:text="button-specific-text"/>
但是当然,没有 include view=""
,只有 include 布局,并且将xml
文件包含布局为 Layout,而不是 View,因此无法设置文本,当我findViewById()
在我的活动中时,它会引用 Layout 而不是一个看法。
有没有类似的东西<include view="...
?
解决方案
您可以使用要多次使用的自定义样式按钮创建布局,并将其包装在<merge>
如下示例的标签中。
include
然后,您可以在任何其他布局中使用该标签一遍又一遍地重复使用它。
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</merge>
在任何其他布局中使用,假设顶部布局命名为my_button.xml
<include layout="@layout/my_button" />
推荐阅读
- apache-kafka - 我能否从 Kafka 主题的特定分区中读取数据
- python - Personality Insight - 分析来自不同用户的评论
- java - 使用 ElementCollection 持久化字符串列表
- sql - 返回由 UNION 关联的两个 SELECT 的 COUNT 行
- html - 如何在面板内放置下拉菜单而不使用锚标签隐藏下拉内容
- c - fork() 中的分段错误
- intel - intel J1900有vsync吗?
- python - 使用 subprocess.call 设置环境变量时捕获“拒绝访问”错误
- android - 在 Oreo 的拨出电话上运行服务
- php - BrightCove API - 如何使用视频 id 以降序获取记录