样式Style
样式是属性的集合,以独立的资源文件存放在xml文件中,并且设置了样式的名字。有点类似与网页设计当中的级联样式CSS的设计思路,实现了设计与内容的分离,以方便样式的继承、覆盖以及重用。
在创建好的项目当中,系统在res/values文件夹中已经创建好了一个styles.xml文件,为了方便管理,我们可以自定义此xml文件的名字,比如文字的可以定义为text_style.xml等,这样一看就知道这个样式里面放置的是什么。
样式的xml格式如下:
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="wrap_content">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
</resources></span>
样式的使用:
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<TextView
style="@style/wrap_content "
android:textColor="#00FF00"
android:text="@string/hello" /> </span>
样式的继承方法如下面的xml所示:
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 样式的格式 -->
<style name="my_text_style">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item>
<item name="android:textColor">@android:color/holo_red_dark</item>
<item name="android:textSize">28sp</item>
</style>
<!-- 样式继承 方法一 -->
<style name="child_text_style" parent="my_text_style">
<item name="android:textColor">@android:color/white</item>
<item name="android:textSize">32sp</item>
<item name="android:textStyle">bold</item>
</style>
<!-- 样式继承 方法二 -->
<style name="my_text_style.child_one_text_style"></style>
<!-- tab切换 RadioButton style -->
<style name="tab_radio_btn_style">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">match_parent</item>
<item name="android:layout_weight">1</item>
<item name="android:background">@drawable/selector_color_radio_tab_bg</item>
<item name="android:button">@null</item>
<item name="android:drawablePadding">2dp</item>
<item name="android:gravity">center_horizontal|bottom</item>
<item name="android:paddingBottom">3dp</item>
<item name="android:paddingTop">3dp</item>
<item name="android:textColor">@drawable/selector_color_home_tab</item>
</style>
</resources></span>
针对应用中所有Activity或者针对某个Activity设置样式,可以通过编辑AndroidManifest.xml来完成。
主题theme
1.设置应用中所有Activity活动的主题
<application android:theme="@style/wrap_content">
<application android:theme="@style/wrap_content">这样,应用中所有Activity中的所有组件都会默认使用包裹布局。
2.设置某个指定的Activity主题
<activity android:theme="@style/wrap_content">
另外,android提供了许多自带的主题样式。例如Theme.Dialog、Theme.Translucent等等。使用方式也很简单
<activity android:theme="@android :style/Theme.Dialog">
3、位置
在Android的frameworks/base/core/res/res/values目录下有一下几个文件:
themes.xml
themes_device_defaults.xml
styles.xml
styles_device_defaults.xml
样式与主题的区别
1.1样式应用于特殊的组件,主题应用与整个应用或整个Activity
1.2主题不仅单单显示内容的风格(大小、颜色),而且可以设置窗口的显示风格
1.3当主题的设置属性与样式的设置属相相同时,系统按样式的设置属性显示