目录(?)[-]

  1. 静态格式
  2. 代码中设定
  3. Style
  4. 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中进行试验,也顺带看看其他效果的设置。

<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",故下图中出现红色下划线,表示拼写错误。

//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文件的相关内容如下:

    <-- 调用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" ... />

相关链接: 我的Android开发相关文章

【转】Pro Android学习笔记(二四):用户界面和控制(12):Style和Theme的更多相关文章

  1. 【转】 Pro Android学习笔记(四十):Fragment(5):适应不同屏幕或排版

    目录(?)[-] 设置横排和竖排的不同排版风格 改写代码 对于fragment,经常涉及不同屏幕尺寸和不同的排版风格.我们在基础小例子上做一下改动,在横排的时候,仍是现实左右两个fragment,在竖 ...

  2. 【转】 Pro Android学习笔记(四二):Fragment(7):切换效果

    目录(?)[-] 利用setTransition 利用setCustomAnimations 通过ObjectAnimator自定义动态效果 程序代码的编写 利用fragment transactio ...

  3. 【转】 Pro Android学习笔记(四八):ActionBar(1):Home图标区

    目录(?)[-] Home Icon 源代码 TextView的滚动 返回主activity或指定activity     ActionBar在Android 3.0 SDK中为平板引入,在4.0中也 ...

  4. 【转】 Pro Android学习笔记(四七):Dialog(4):一些补充和思考

    目录(?)[-] 编程思想封装接口 fragment和activity以其他fragment之间的通信 编程思想:封装接口 在小例子中,fragment会调用activity的onDialogDone ...

  5. 【转】 Pro Android学习笔记(四三):Fragment(8):再谈Transaction和管理器

    目录(?)[-] Transaction的一些操作 再谈FragmentManager 调用其他fragment的方法 唤起activity 唤起fragment和相互通信 一些其它 Transact ...

  6. 【转】Pro Android学习笔记(四):了解Android资源(下)

    处理任意的XML文件 自定义的xml文件放置在res/xml/下,可以通过R.xml.file_name来获取一个XMLResourceParser对象.下面是xml文件的例子: <rootna ...

  7. 【转】 Pro Android学习笔记(二九):用户界面和控制(17):include和merge

    目录(?)[-] xml控件代码重用include xml控件代码重用merge 横屏和竖屏landsacpe portrait xml控件代码重用:include 如果我们定义一个控件,需要在不同的 ...

  8. Pro Android学习笔记 ActionBar(1):Home图标区

     Pro Android学习笔记(四八):ActionBar(1):Home图标区 2013年03月10日 ⁄ 综合 ⁄ 共 3256字 ⁄ 字号 小 中 大 ⁄ 评论关闭 ActionBar在A ...

  9. 【转】 Pro Android学习笔记(五六):配置变化

    目录(?)[-] Activity的destorycreate过程 Fragment的destorycreate过程 onSaveInstanceState saveFragmentInstanceS ...

  10. 【转】 Pro Android学习笔记(二二):用户界面和控制(10):自定义Adapter

    目录(?)[-] 设计Adapter的布局 代码部分 Activity的代码 MyAdapter的代码数据源和构造函数 MyAdapter的代码实现自定义的adapter MyAdapter的代码继续 ...

随机推荐

  1. Java语言实现简单FTP软件------>远程文件管理模块的实现(十)

    首先看一下界面: 1.远程FTP服务器端的文件列表的显示 将远程的当前目录下所有文件显示出来,并显示文件的属性包括文件名.大小.日期.通过javax.swing.JTable()来显示具体的数据.更改 ...

  2. ExtASPNet web.config

    [转CSDN]:http://download.csdn.net/download/mcqq123321/4607708 修改 Web.config 打开 web.config,在 configura ...

  3. Windows存储管理之磁盘结构详解

    Windows磁盘结构: Windows的主流磁盘结构分为MBR和GPT两种.MBR是早期Windows的唯一选择,但是随着物理磁盘的容量不断增大.GPT结构成为目前的主流,最大支持超过2TB的容量, ...

  4. awk substr()函数

    awk 里的substr函数用法举例: 要截取要截取的内容1: F115!16201!1174113017250745 10.86.96.41 211.140.16.1 200703180718F12 ...

  5. 有关numpy.random下的API具体含义

    1.numpy.random.random(size=None) Return random floats in the half-open interval [0.0, 1.0). 返回size大小 ...

  6. 让input表单输入框不记录输入过信息的方法

    有过表单设计经验的朋友肯定知道,当我们在浏览器中输入表单信息的时候,往往input文本输入框会记录下之前提交表单的信息,以后每次只要双击input文本输入框就会出现之前输入的文本,这样有时会觉得比较方 ...

  7. 牛客练习赛13 A 幸运数字Ⅰ 【暴力】

    题目链接 https://www.nowcoder.com/acm/contest/70/A 思路 暴力每一个子串 用 MAP 标记一下 然后 最后 遍历一遍 MAP 找出 出现次数最多 并且 字典序 ...

  8. css 分析

    .important.warning {background:silver;} .important .warning {background:silver;} //上面有什么区别? //1.2个选择 ...

  9. [算法]打印N个数组的整体最大Top K

    题目: 有N个长度不一的数组,所有的数组都是有序的,请从大到小打印这N个数组整体最大的前K个数. 例如: 输入含有N行元素的二维数组代表N个一维数组. 219,405,538,845,971 148, ...

  10. 查找mysql的cnf文件位置

    mysql --help|grep 'my.cnf' 查看mysql启动时读取配置文件的默认目录 命令 mysql --help|grep 'my.cnf' 输出 order of preferenc ...