在android原生系统中,提供了大量的样式和主题背景,借助于系统的样式和主题背景,我们可以将应用设计的细节与界面的结构和行为分开,其作用类似于网页设计中的样式表。那么,今天我们就来看一看android 原生系统中的样式和主题背景。
样式
样式是一个属性集合,用于指定单个View的外观。样式可以指定字体样式、字号、背景颜色等属性,例如:
(1)在values/styles.xml文件里面设置样式格式
<!-- 设置底部的样式 -->
<style name="home_bottom_tab_style">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">@dimen/dp50</item>
<item name="android:textColor">@color/main_bottom_text_color</item>
<item name="android:layout_weight">1</item>
<item name="android:gravity">center</item>
<item name="android:button">@null</item>
<item name="android:paddingTop">@dimen/dp6</item>
<item name="android:textSize">@dimen/dp10</item>
</style>
(2)直接引用样式格式
<RadioButton
android:id="@+id/frag_main"
style="@style/home_bottom_tab_style"
android:layout_marginLeft="@dimen/dp40"
android:layout_marginRight="@dimen/dp40"
android:drawableTop="@drawable/main_bottom_home"
android:text="@string/main" />
主题背景
主题背景是应用于整个应用、Activity或视图层次结构,而非仅仅应用于单个视图的属性集合。当应用主题背景时,应用或Activity中的每个视图都会应用其支持的每个主题背景属性。除了这些之外,主题还可以将样式应用于非视图元素,例如状态栏和标题栏。例如:
(1)在values/styles.xml文件中设置主题样式
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowBackground">@color/windowBackground</item>
</style>
(2)直接引用主题背景
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:maxAspectRatio="2.4"
android:requestLegacyExternalStorage="true"
android:resizeableActivity="false"
android:roundIcon="@mipmap/ic_launcher"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true"
tools:ignore="AllowBackup,GoogleAppIndexingWarning">
</application>
注意:样式和主题都是在res/values/styles.xml文件中声明
创建并应用样式步骤
1.在res/values/styles.xml文件中使用唯一标识样式的名称添加<style>元素
2.为要定义的每个样式属性添加一个<item>元素
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="GreenText" parent="TextAppearance.AppCompat">
<item name="android:textColor">#00FF00</item>
</style>
</resources>
3.在对应的位置进行调用
<TextView
style="@style/GreenText"
... />
只要作用的视图接受,该样式中指定的每个属性都会应用于该视图。而对于不接受的属性,视图则会将其忽略。注意只有添加了styles属性的元素才会收到这些样式属性,任何子视图都不会应用这些样式。如果希望子视图继承样式,应改为应用具有android:theme样式的属性。
扩展和自定义样式
为保持与平台界面样式的兼容性,应该始终通过扩展框架或支持库中的现有样式来创建自己的样式。如需扩展样式,请使用parent属性指定要扩展的样式,比如继承Android平台的默认文本外观,并对其中的细微进行修改。同时为了更高版本的兼容,建议使用支持库来实现,不要包含@android:style/部分,而改用AppCompat。如下:
<style name="GreenText" parent="TextAppearance.AppCompat">
<item name="android:textColor">#00FF00</item>
</style>
样式层次结构
在选择如何为应用设置样式时,需要考虑样式的层次结构。一般来说,在实际的开发过程中很有可能在多个位置指定了相同的属性,那么最终会展示哪个样式呢。按照优先级从高到低排列为:
1.通过文本 span 将字符或段落级样式应用到 TextView 派生的类
2.通过代码设置
3.将单独的属性直接应用到 View
4.将样式应用到 View
5.使用系统默认样式
6.使用主题背景
7.应用某些特定于 View 的样式
最后的结果按照优先级来,谁的优先级最高,最终呈现出来的就是什么样子。
自定义默认主题
当我们创建一个项目的时候,系统会默认帮我们创建一个主题,然后在我们的Application里面引用。如下所示:
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<application
android:name=".MyApplication"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
</application>
其中,在styles.xml文件中主题所设置的都是系统颜色值,具体来说可以看下面这两张图
除此之外,还有一些可供设置的颜色值:
(1)colorControlHighlight:所有可点击 Views 触摸状态下的 Ripple(涟漪)效果。仅作用于 Lollipop 及更高版本。
(2)colorButtonNormal:Button normal 状态下的背景色。
添加特定于版本的样式
由于android 手机的碎片化,导致市面上各个版本的系统手机都有可能存在,那么,如何在众多的机型中去完美匹配呢?其实我们只需要在合适的版本上去新增styles.xml文件,然后在此基础上去添加我们想要的额外的样式就可以了。比如,我们想使用在5.0手机样式上的功能,可以新建一个values-v21/styles.xml,然后在此基础上去添加。举个例子如下:
在res/values/styles.xml样式
<resources>
<!-- base set of styles that apply to all versions -->
<style name="BaseAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/primaryColor</item>
<item name="colorPrimaryDark">@color/primaryTextColor</item>
<item name="colorAccent">@color/secondaryColor</item>
</style>
<!-- declare the theme name that's actually applied in the manifest file -->
<style name="AppTheme" parent="BaseAppTheme" />
</resources>
然后,在 res/values-v21/styles.xml 中添加特定于版本的样式,如下所示
<resources>
<!-- extend the base theme to add styles available only with API level 21+ -->
<style name="AppTheme" parent="BaseAppTheme">
<item name="android:windowActivityTransitions">true</item>
<item name="android:windowEnterTransition">@android:transition/slide_right</item>
<item name="android:windowExitTransition">@android:transition/slide_left</item>
</style>
</resources>
常见的主题风格
android:theme="Theme.Light" 背景为白色
android:theme="Theme.Light.NoTitleBar" 白色背景并无标题栏
android:theme="Theme.Light.NoTitleBar.Fullscreen" 白色背景,无标题栏,全屏
android:theme="Theme.Black" 背景黑色
android:theme="Theme.Black.NoTitleBar" 黑色背景并无标题栏
android:theme="Theme.Black.NoTitleBar.Fullscreen" 黑色背景,无标题栏,全屏
android:theme="Theme.Wallpaper" 用系统桌面为应用程序背景
android:theme="Theme.Wallpaper.NoTitleBar" 用系统桌面为应用程序背景,且无标题栏
android:theme="Theme.Wallpaper.NoTitleBar.Fullscreen" 用系统桌面为应用程序背景,无标题栏,全屏
android:theme="Translucent" 半透明
android:theme="Theme.Translucent.NoTitleBar" 半透明、无标题栏
android:theme="Theme.Translucent.NoTitleBar.Fullscreen" 半透明、无标题栏、全屏
android:theme="Theme.Panel"
android:theme="Theme.Light.Panel"
关于theme的讲解就到这里了,欢迎大家在下面留言