Android上的Style分为了两个方面:
1,Theme是针对窗体级别的,改变窗体样式;
2,Style是针对窗体元素级别的,改变指定控件或者Layout的样式。
Android系统的themes.xml和style.xml以及attrs.xml(位于系统源代码frameworks\base\core\res\res\values\)包含了很多系统定义好的style,建议在里面挑个合适的,然后再继承修改。
- 风格是一个包含一种或者多种格式化属性的集合,你可以将其用为一个单位用在布局XML单个元素当中。比如,你可以定义一种风格来定义文本的字号大小和颜色,然后将其用在View元素的一个特定的实例。
- 主题是一个包含一种或者多种格式化属性的集合,你可以将其为一个单位用在应用中所有的Activity当中或者应用中的某个Activity当 中。比如,你可以定义一个主题,它为window frame和panel 的前景和背景定义了一组颜色,并为菜单定义可文字的大小和颜色属性,你可以将这个主题应用在你程序当中所有的Activity里。
-----------------------------------------------------------------------------------
一,Theme主题:
应用于Application、Activity,主题Theme就是用来设置界面UI风格,可以设置整个应用或者某个活动Activity的界面风格。在Android SDK中内置了下面的Theme,可以按标题栏Title Bar和状态栏Status Bar是否可见来分类:
1.1.系统自带主题
<span style="font-family:Times New Roman;font-size:14px;">android:theme="@android:style/Theme.Dialog" : Activity显示为对话框模式
android:theme="@android:style/Theme.NoTitleBar" : 不显示应用程序标题栏
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" : 不显示应用程序标题栏,并全屏
android:theme="@android:style/Theme.Light ": 背景为白色
android:theme="@android:style/Theme.Light.NoTitleBar" : 白色背景并无标题栏
android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen" : 白色背景,无标题栏,全屏
android:theme="@android:style/Theme.Black" : 背景黑色
android:theme="@android:style/Theme.Black.NoTitleBar" : 黑色背景并无标题栏
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" : 黑色背景,无标题栏,全屏.全屏将会没有
android:theme="@android:style/Theme.Wallpaper" : 用系统桌面为应用程序背景
android:theme="@android:style/Theme.Wallpaper.NoTitleBar" : 用系统桌面为应用程序背景,且无标题栏
android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen" : 用系统桌面为应用程序背景,无标题栏,全屏
android:theme="@android:style/Theme.Translucent : 透明背景
android:theme="@android:style/Theme.Translucent.NoTitleBar" : 透明背景并无标题
android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" : 透明背景并无标题,全屏
android:theme="@android:style/Theme.Panel ": 面板风格显示
android:theme="@android:style/Theme.Light.Panel" : 平板风格显示</span>
跟踪其中Theme.Black的源码,可以看到如下
<span style="font-family:Times New Roman;font-size:14px;"> <!-- Variant on {@link #Theme} that ensures the background is
completely black. This is useful for things like image viewers and
media players. If you want the normal (dark background) theme
do <em>not</em> use this, use {@link #Theme}. -->
<style name="Theme.Black">
<item name="android:windowBackground">@android:color/black</item>
<item name="android:colorBackground">@android:color/black</item>
</style></span>
该配置文件定义了背景色为黑色
1.2.使用Theme的方式
1.2.1,在XML中添加:
<span style="font-family:Times New Roman;font-size:14px;"><application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Light.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application></span>
1.2.2,通过代码添加
在Activity中的onCreate()方法中设置,注意,setTheme()方法要放在setContentView()之前调用,否则没有效果。
<span style="font-family:Times New Roman;font-size:14px;"> @Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme_Translucent_NoTitleBar);
setContentView(R.layout.main);
}</span>
1.3,自定义主题
根据需要,我们可能需要自己定义满足我们需求的主题。
<span style="font-family:Times New Roman;font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomTheme">
<item name="android:windowNoTitle">true</item>
<item name="windowFrame">@drawable/screen_frame</item>
<item name="windowBackground">@drawable/screen_background_white</item>
<item name="panelTextSize">14sp</item>
</style>
</resources></span>
1.3.2 Theme继承
Android系统提供了需要定义好的主题资源,我们在定义使用themes时,可以继承使用系统主题资源
<span style="font-family:Times New Roman;font-size:14px;"> <style name="Sample" parent="@android:style/Theme.Black">
<item name="android:colorBackground">@android:color/holo_orange_dark</item>
</style></span>
在继承使用自己定义的主题资源时,不需要使用parent指定资源,可以使用如下继承方式
<span style="font-family:Times New Roman;font-size:14px;"> <style name="Sample.NoTitleBar">
<item name="android:windowNoTitle">true</item>
</style></span>
二,style风格:
应用于组件View。经常在代码开发中会重复去设置很多View的布局参数,然而很多组件是拥有相同的配置参数,如
上图中手机备份、闪电互传等就具有很多相同的配置参数,可以设置为一组Style。
风格是一个包含一种或者多种格式化属性的集合,你可以将其用为一个单位用在布局XML单个元素当中。比如,你可以定义一种风格来定义文本的字号大小和颜色,然后将其用在View元素的一个特定的实例。
2.1 基本用法
Android采用XML实现界面布局,Android中的Style类似CSS,是能够真正做到网页表现与内容分离,View配置中也提供了html中如id、name属性一样的功能标签。
<span style="font-family:Times New Roman;font-size:14px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test1"
android:textColor="#FFFFFFFF"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test2"
android:textColor="#FFFFFFFF"
android:textSize="16sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test3"
android:textColor="#FFFFFFFF"
android:textSize="16sp" />
</LinearLayout>
</span>
在上面的布局文件中,三个TextView的android:layout_width、android:layout_height、android:textColor、android:textSize样式属性都一样,这种情况下我们就可以单独定义一个style,以一种更优雅的做法来代替上面的写法。
tmp_style.xml
<span style="font-family:Times New Roman;font-size:14px;"><resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="TextStyle">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#FFFFFFFF</item>
<item name="android:textSize">16sp</item>
</style>
</resources>
</span>
注:style文件必须放在values-*目录中
接下来就是在View配置中使用style了
<span style="font-family:Times New Roman;font-size:14px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv1"
style="@style/TextStyle"
android:text="test1" />
<TextView
android:id="@+id/tv2"
style="@style/TextStyle"
android:text="test2" />
<TextView
android:id="@+id/tv3"
style="@style/TextStyle"
android:text="test4" />
</LinearLayout></span>
使用时只需要使用style属性引用我们定义好的style即可,这种方式可以定义避免重复性的工作,简化工作。
2.2 Styles继承
Android系统为我们提供了一整套的style,比如说显示字符串最基本的style是TextAppearance,他定义的一些最基本的属性
<span style="font-family:Times New Roman;font-size:14px;"> <style name="TextAppearance">
<item name="textColor">?textColorPrimary</item>
<item name="textColorHighlight">?textColorHighlight</item>
<item name="textColorHint">?textColorHint</item>
<item name="textColorLink">?textColorLink</item>
<item name="textSize">16sp</item>
<item name="textStyle">normal</item>
</style></span>
还有许多各方面的style,大家可以参见:
https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/styles.xml
如果只想改变TextAppearance中的某一项属性时,这时可以使用继承来实现
<span style="font-family:Times New Roman;font-size:14px;"> <style name="Sample_1" parent="@android:style/TextAppearance">
<item name="android:textColor">#909090</item>
</style></span>
也可以继承自己定义的style,继承自己定义的style时,不需要使用parent属性来指定
<span style="font-family:Times New Roman;font-size:14px;"> <style name="Sample_1.Small">
<item name="android:textSize">12sp</item>
</style></span>
三,Attrs属性:
attrs看字面意思就是一组属性的集合,那attrs有什么用呢,在自定义View的时候,一般会自定义一些属性,通过构造方法中AttributeSet参数的封装,让我们能够获取到为View配置的属性,至于怎么操作,大家可以参看http://developer.android.com/training/custom-views/index.html上介绍的这个例子
四,Attrs、Style、Theme之间的联系
第一我们定义一个attrs,并指定好类型
<span style="font-family:Times New Roman;font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="gdDrawableWidth" format="dimension" />
<attr name="gdDrawableHeight" format="dimension" />
<attr name="gdActionBarTitleColor" format="color" />
<attr name="gdActionBarBackground" format="reference" />
</resources></span>
然后就可以在style中进行引用了
<span style="font-family:Times New Roman;font-size:14px;"><resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="TextStyle">
<item name="android:layout_width">?attr/gdDrawableWidth</item>
<item name="android:layout_height">?attr/gdDrawableHeight</item>
<item name="android:textColor">?attr/gdActionBarTitleColor</item>
<item name="android:textSize">16sp</item>
</style>
</resources></span>
每一个引用需要指向一个实际资源,这里可以在themes 中定义
<span style="font-family:Times New Roman;font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="android:Theme.Light">
<item name="gdDrawableWidth">@dimen/gd_drawable_width</item>
<item name="gdDrawableHeight">@dimen/gd_drawable_height</item>
<item name="gdActionBarTitleColor">@color/gd_bar_title_color</item>
<item name="gdActionBarBackground">@color/gd_bar_backgroud</item>
</style>
</resources></span>
<span style="font-family:Times New Roman;font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="gd_bar_title_color">#FFFFFFFF</color>
<color name="gd_bar_backgroud">#FF777777</color>
</resources></span>
<span style="font-family:Times New Roman;font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="gd_drawable_width">200dp</dimen>
<dimen name="gd_drawable_height">40dp</dimen>
</resources></span>
列表如下:
https://github.com/cyrilmottier/GreenDroid
http://developer.android.com/guide/topics/ui/themes.html
http://developer.android.com/training/custom-views/index.html
分享到:
相关推荐
利用Android自身支持的不同Style中可复写相同的attribute的值的特性,通过代码动态设置不同的Style来达到不同主题的切换效果。它支持静态设置控件使用主题元素的方式——layout的xml中定义控件时使用,也支持程序...
真正的支持无缝换肤/夜间模式的Android框架,配合theme和换肤控件框架可以做到无缝切换换肤(无需重启应用和当前页面)。 该应用框架可以实现无缝换肤/切换夜间模式的需求,需要在换肤/切换夜间模式的界面只需要...
attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </com.google.android.material.appbar.AppBarLayout> <Button android:id="@+id/next" android:layout_width="100dp" android:layout_height...
style name = " Theme.YourApp " parent = " @android:style/Theme.Material.Light " > < item xss=removed>?android:attr/textColorSecondary</ item> < item xss=removed>?android:attr/colorAccent</ item>&...
2.holo风格的dialog包含两种holo:Theme.AlertDialogPro.Holo和Theme.AlertDialogPro.Holo.Light只需在style文件中加入alertDialogProTheme属性:<style name="AppTheme" parent="AppBaseTheme"> ... ...
基本使用:Step1:在manifest.xml中给使用CityPicker 的activity添加主题android:theme="@style/DefaultCityPickerTheme" android:name=".MainActivity" android:theme="@style/DefaultCityPickerTheme"> ...
import android.content.Context; import android.content.res.ColorStateList; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics...
# RapidDevelop-Android快速开发框架 - 框架持续更新中 - 这个框架是从平时项目里用的比较多的框架里整合而来 - 对本项目感兴趣的可以一起研究喜欢的朋友欢迎star - 同时也欢迎大家的宝贵意见issues - 如果大家...
app:theme="@style/CustomToolbarTheme" /> android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/toolbar" android:layout_centerInParent=...