zoukankan      html  css  js  c++  java
  • 【转】Pro Android学习笔记(二四):用户界面和控制(12):Style和Theme

    目录(?)[-]

    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可以在sdkplatformsandroid-xxdata esvaluesstyles.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在sdkplatformsandroid-xxdata esvalues hemes.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开发相关文章

  • 相关阅读:
    一款非常好用的范围滑动插件
    设置滚动条样式
    Qml 定义 constant
    qml 中 使用 shader
    Qt ImageProvider 的使用
    qt 汉化 国际化
    qt rcc 使用
    CentOS7/RHEL7 pacemaker+corosync高可用集群搭建
    Ubunt平台Qt出现:-1: error: cannot find -lgl
    排序-堆排序
  • 原文地址:https://www.cnblogs.com/blongfree/p/5047938.html
Copyright © 2011-2022 走看看