【转】Pro Android学习笔记(二四):用户界面和控制(12):Style和Theme
2015-12-15 13:15
411 查看
目录(?)[-]
静态格式
代码中设定
Style
Theme
<string name="ui_styleText_1"><i>Static</i> style <u>in</u> a <b>TextView</b>. <strike>strike</strike></string>
我们在XML中进行试验,也顺带看看其他效果的设置。
![](http://ww2.sinaimg.cn/large/5cf79a90gw1e8bh206hrbj20dg06taai.jpg)
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="web|email"
android:text="Please visit www.androidbook.com for more help on using Android."
android:minLines="3"
android:typeface="serif" />
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/ui_styleText_1"/>
![](http://ww2.sinaimg.cn/large/5cf79a90gw1e8biwa7qznj20sf09s3zq.jpg)
//TextView需要先指定BufferType,才能通过getText( )获取spannable对象。
TextView tv = (TextView)findViewById(R.id.ui_style_tv);
tv.setText("This text is stored in a Spannable", TextView.BufferType.SPANNABLE);
Spannable spanTv = (Spannable)tv.getText();
//通过setSpan(),对text中的某个范围,本例0~7的字符进行处理
spanTv.setSpan(new BackgroundColorSpan(Color.RED), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spanTv.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//EditText可以直接获取通过getText()获取Spannable对象
EditText et = (EditText)findViewById(R.id.ui_style_ed);
et.setText("Styling the content of an EditText dynamically");
Spannable spanEt = (Spannable)et.getText();
spanEt.setSpan(new BackgroundColorSpan(Color.RED), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spanEt.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 例子1:定义名字为“MyErrorText”的style, 设置颜色和字体,还顺带设置了width和height,不用每次写这么烦 -->
<style name="MyErrorText">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#FF0000</item>
<item name="android:typeface">monospace</item>
</style>
<!-- 例子2:style树状层次结构:style提供一个很方便的层次结构,可以一层一层地通过“.”进行扩展 -->
<style name="MyErrorText.Danger">
<item name="android:textStyle">bold</item>
</style>
<!-- 例子3:对于扩展android自带的style,不能采用“.”,而是用parent。android自带的style可以在sdk\platforms\android-xx\data\res\values\styles.xml 查看 -->
<style name="MyText" parent="@android:style/TextAppearance.Small">
<item name="android:textColor">#FF00FF</item>
</style>
</resources>
layout xml文件的相关内容如下:
![](http://ww2.sinaimg.cn/large/5cf79a90gw1e8kyuvbee8j20de08vmxu.jpg)
<-- 调用style,和其他属性不同,前面没有android: -->
<TextView style="@style/MyErrorText"
android:text="Error: No Error here." />
<-- 调用居于层次结构的style -->
<TextView style="@style/MyErrorText.Danger"
android:text="Fatal Error: Test...." />
<-- 调用android系统自定义的style-->
<TextView style="@android:style/TextAppearance.Holo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Android Default Style : TextAppearance.Holo" />
<-- 测试style的例子3的效果 -->
<TextView style="@style/MyText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Information: my text" />
<-- 对于系统定义的Theme,我们可以只使用其中的某个属性 -->
<EditText android:id="@+id/ui_style_ed2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="?android:textColorSecondary"
android:text="@string/ui_styleTest"/>
<style name="MyTheme" parent="@android:style/Theme">
<item name="android:textColor">#666666</item>
</style>
Theme在AndroidManifest.xml中设置。
对于activity:
<activity ... android:theme="@style/MyTheme" ... />
对于application:
<application .... android:theme="@style/MyTheme" ... />
![](http://ww3.sinaimg.cn/large/5cf79a90gw1e8kyuuuc4dj20mv0hyacn.jpg)
相关链接: 我的Android开发相关文章
静态格式
代码中设定
Style
Theme
静态格式
在res/values中设置静态的Style,在资源中设置静态Style可使用的HTML格式有<i> <u> <b> <sup> <sub> <strike> <big> <small> <monospace>。<string name="ui_styleText_1"><i>Static</i> style <u>in</u> a <b>TextView</b>. <strike>strike</strike></string>
我们在XML中进行试验,也顺带看看其他效果的设置。
![](http://ww2.sinaimg.cn/large/5cf79a90gw1e8bh206hrbj20dg06taai.jpg)
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="web|email"
android:text="Please visit www.androidbook.com for more help on using Android."
android:minLines="3"
android:typeface="serif" />
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/ui_styleText_1"/>
代码中设定
在代码中通过Spannable中设定Style。如下图所示。对于EditText,我们在xml中设置了 android:inputType="text|textAutoCorrect|textAutoComplete|textMultiLine",故下图中出现红色下划线,表示拼写错误。![](http://ww2.sinaimg.cn/large/5cf79a90gw1e8biwa7qznj20sf09s3zq.jpg)
//TextView需要先指定BufferType,才能通过getText( )获取spannable对象。
TextView tv = (TextView)findViewById(R.id.ui_style_tv);
tv.setText("This text is stored in a Spannable", TextView.BufferType.SPANNABLE);
Spannable spanTv = (Spannable)tv.getText();
//通过setSpan(),对text中的某个范围,本例0~7的字符进行处理
spanTv.setSpan(new BackgroundColorSpan(Color.RED), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spanTv.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//EditText可以直接获取通过getText()获取Spannable对象
EditText et = (EditText)findViewById(R.id.ui_style_ed);
et.setText("Styling the content of an EditText dynamically");
Spannable spanEt = (Spannable)et.getText();
spanEt.setSpan(new BackgroundColorSpan(Color.RED), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spanEt.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Style
如果相同的格式用于多个控件,每个控件都写一遍,很麻烦,可以定义成为style,并在控件中设置style。style在res/values/中定义,如下所示。<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 例子1:定义名字为“MyErrorText”的style, 设置颜色和字体,还顺带设置了width和height,不用每次写这么烦 -->
<style name="MyErrorText">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#FF0000</item>
<item name="android:typeface">monospace</item>
</style>
<!-- 例子2:style树状层次结构:style提供一个很方便的层次结构,可以一层一层地通过“.”进行扩展 -->
<style name="MyErrorText.Danger">
<item name="android:textStyle">bold</item>
</style>
<!-- 例子3:对于扩展android自带的style,不能采用“.”,而是用parent。android自带的style可以在sdk\platforms\android-xx\data\res\values\styles.xml 查看 -->
<style name="MyText" parent="@android:style/TextAppearance.Small">
<item name="android:textColor">#FF00FF</item>
</style>
</resources>
layout xml文件的相关内容如下:
![](http://ww2.sinaimg.cn/large/5cf79a90gw1e8kyuvbee8j20de08vmxu.jpg)
<-- 调用style,和其他属性不同,前面没有android: -->
<TextView style="@style/MyErrorText"
android:text="Error: No Error here." />
<-- 调用居于层次结构的style -->
<TextView style="@style/MyErrorText.Danger"
android:text="Fatal Error: Test...." />
<-- 调用android系统自定义的style-->
<TextView style="@android:style/TextAppearance.Holo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Android Default Style : TextAppearance.Holo" />
<-- 测试style的例子3的效果 -->
<TextView style="@style/MyText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Information: my text" />
<-- 对于系统定义的Theme,我们可以只使用其中的某个属性 -->
<EditText android:id="@+id/ui_style_ed2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="?android:textColorSecondary"
android:text="@string/ui_styleTest"/>
Theme
在上面的例子中已经提到theme。使用style可以避免在逐个控件中进行控件属性的描述,如果修改可以只在一个地方进行修改,提供了很大的便捷,但是仍需要在每个控件中进行style指定。如果希望属性能够在整个activity或者整个application都实用,可以采用theme。theme和style概念上很相似,在定义属性时,一样采用style在res/vaules/的xml文件中描述。如下面的例子。android系统自定义的theme在sdk\platforms\android-xx\data\res\values\themes.xml中定义。<style name="MyTheme" parent="@android:style/Theme">
<item name="android:textColor">#666666</item>
</style>
Theme在AndroidManifest.xml中设置。
对于activity:
<activity ... android:theme="@style/MyTheme" ... />
对于application:
<application .... android:theme="@style/MyTheme" ... />
![](http://ww3.sinaimg.cn/large/5cf79a90gw1e8kyuuuc4dj20mv0hyacn.jpg)
相关链接: 我的Android开发相关文章
相关文章推荐
- 【转】Pro Android学习笔记(二三):用户界面和控制(11):其他控件
- Android Intent.FLAG_NEW_TASK详解,包括其他的标记的一些解释
- Android开发艺术2之Activity的启动模式
- 【转】 Pro Android学习笔记(二二):用户界面和控制(10):自定义Adapter
- 【转】 Pro Android学习笔记(二一):用户界面和控制(9):Gallery和SimpleAdapter
- 【转】 Pro Android学习笔记(二十):用户界面和控制(8):GridView和Spinner
- 【转】 Pro Android学习笔记(十九):用户界面和控制(7):ListView
- 【转】Pro Android学习笔记(十八):用户界面和控制(6):Adapter和AdapterView
- 【转】Pro Android学习笔记(十七):用户界面和控制(5):日期和时间控件
- 【转】Pro Android学习笔记(十六):用户界面和控制(4):ImageView控件
- 【转】Pro Android学习笔记(十五):用户界面和控制(3):Button控件
- Android中内存泄露的原因分析:
- 【转】Pro Android学习笔记(十四):用户界面和控制(2):Text类控制
- Android开发艺术探索读书笔记(第一章)
- adb 连接不上的问题
- android反编译相关
- Android-性能优化01-布局优化&绘制优化&内存泄露优化
- Android—图片上传压缩工具类
- Android里面主菜单里面设置菜单列表,单项选择,多项选择的实现
- Android布局之LinerLayout点滴