首页 > 解决方案 > Android在代码/动态中为Views设置不同的样式

问题描述

我想在代码中动态地将自定义样式添加到文本视图。当只有 textview 有问题时,我可以使用以下解决方法来执行此操作:

android在代码中设置样式

但是,如果我尝试这种方法并创建一个带有 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>

如何扩展此解决方案,使其适用于多个文本视图和多个样式?

标签: androidandroid-layoutandroid-xmlandroid-styles

解决方案


推荐阅读