在一个位置为布局中的所有 TextView 元素设置样式



有没有办法将样式应用于布局xml中特定类型的所有元素,而无需实际将样式属性添加到所有元素?

我在 GridLayout 中有很多 TextView 元素。如果我能说所有 TextView 元素都应该具有"CellFont"样式,而不必实际将该语句放入每个 TextView 元素中,那就太好了。

解决方案如下:

将 GridLayoutCellFont 样式添加到样式.xml并将该样式作为 android:theme 添加到应用程序清单中的活动。

风格.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="GridLayoutCellFont">
        <item name="android:textSize">22dp</item>
    </style>
</resources>

清单:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:theme="@style/GridLayoutCellFont"
        android:label="@string/app_name"
        android:name=".TestLayoutsActivity" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

可以使用样式或设置新主题的两个选项 在样式和主题教程中阅读更多内容

设置Android样式或 theme.in RAW可以定义样式

最新更新