android - Android在代码/动态中为Views设置不同的样式
问题描述
我想在代码中动态地将自定义样式添加到文本视图。当只有 textview 有问题时,我可以使用以下解决方法来执行此操作:
但是,如果我尝试这种方法并创建一个带有 2 个 textview 的 template.xml 文件,每个都应用了不同的样式,这将不再有效。
即我的模板文件将看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is a template"
style="@style/my_style" />
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is another template"
style="@style/my_style2" />
</LinearLayout>
如何扩展此解决方案,使其适用于多个文本视图和多个样式?
解决方案
推荐阅读
- python-3.x - “导入_pjsua”错误。DLL 加载失败:找不到特定模块
- terraform - 使用 address_prefixes 错误创建子网
- wpf - WPF 窗口未从 PowerShell 更新
- python - u'articles' 不是注册的命名空间
- javascript - RxJS 多个条件调用
- c++ - 将 nullptr 转换为 std::span
- string - Linting:比较 Verilog 参数和常量字符串
- javascript - 关闭鼠标悬停时打开的弹出菜单时防止恢复焦点
- c - 如何在动态链接库中调用全局变量?
- python - 如何使用循环创建和返回 5 行数据框