Android 样式与主题

Table of Contents

1 Android 样式与主题

1.1 设置样式

假设我们给一个按钮设置样式,在 res/values/style.xml

<style name="BeatBoxButton">
  <item name="android:background">@color/dark_blue</item>
</style>

在需要设置的按钮布局中

<Button
    style="@style/BeatBoxButton"/>

这样就完成了样式的设置

1.2 继承样式

样式支持继承,我们来继承上面的样式,创建一个新的样式

<style name="BeatBoxButton.Strong">
  <item name="android:textStyle">bold</item>
</style>

也可以使用

<style name="StrongBeatBoxButton" parent="@style/BeatBoxButton">
  <item name="android:textStyle">bold</item>
</style>

1.3 使用主题

主题也是一种样式,他必须继承自 Theme 下的主题,这里定义一个 style

<style name="BoxButtonTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
  <item name="android:background">@color/black</item>
  <item name="android:textColor">@color/white</item>
  <item name="android:textStyle">bold</item>
</style>

AndroidManifest.xml 中,在 activity 标签添加

<activity android:name=".MainActivity" android:theme="@style/BoxButtonTheme">

这样以后,在 .MainActivity 下的所有控件都会使用 BoxButtontheme 主题中定义的样式
同理,在 application 标签添加主题,旗下的 activity 都会使用该主题

Author: Steiner

Created: 2022-03-04 五 15:21

Validate