Android的样式与主题

在开发过程中,Style和Theme是不可缺少的一部分,本文主要讲述了如何创建样式和主题。

Style:定义控件属性

1. 创建样式步骤如下:

  • 在res/Values目录下新建一个Android XML File,名为styles.xml。文件名可以自定义
  • 定义一个字体大小为20dp,颜色为#FF0000的样式,在xml文件中写入如下代码
    <?xml version="1.0" encoding="UTF-8"?>
    <resources>

        <style name="firstSytle">
            <item name="android:textSize">20dp</item>
            <item name="android:textColor">#FF0000</item>
        </style>

    </resources>
  • 引用TextView控件
    <TextView style="@style/firstSytle">第一个样式</TextView>
  • 这样Style就创建成功了,在R文件中会生成标识。

 2.重用父样式,只需要在Style标签中加入parent 属性,让它的值为父样式的name

  <style name="secondStyle" parent="firstSytle">
        <item name="android:background">#00FF00</item>
    </style>

或者用“.”来标识

 <style name="firstSytle.secondStyle" >
        <item name="android:background">#00FF00</item>
    </style>

Theme:可以理解为用来定一个Activity或者一个应用程序的样式

1.同样在Theme文件中创建Style

   <style name="firstTheme">
        <item name=android:textColor>#FF0000</item>
    
</style>

2. 主题的调用,如果使用在整个系统中,可以在AndroidManifest.xml的Application中指定

 android:theme="firstThemt",如果使用在某个Activity中,就放在Activity中。
原文地址:https://www.cnblogs.com/Enno/p/2366929.html