当前位置: 代码迷 >> Android >> Android Style与Theme的施用
  详细解决方案

Android Style与Theme的施用

热度:78   发布时间:2016-05-01 13:32:53.0
Android Style与Theme的应用
使用样式
1.通过HTML标记,可以在字符串资源中直接使用HTML标记来达到修改样式,如
<string name="text"><i>Hello</i> <b>World</b></string>

2.通过代码方式(Spannable对象)修改样式.
获得TextView的Spannable对象的方法是先setText("something", TextView.BufferType.SPANNABLE), 然后使用getText()即可返回Spannable对象.
EditText et = (EditText)findViewById(R.id.et);et.setText("hello world");Spannable spn = (Spannable) et.getText();spn.setSpan(new BackgroundColorSpan(Color.RED), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);spn.setSpan(new StyleSpan(android.graphics.Typeface.BLOD_ITALIC), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);


3.通过XML来创建公用的样式文件
在/res/values/下创建一个XML文件如下:
<?xml version="1.0" encoding="utf-8"?> <resources>     <style name="errorText">         <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> </resources>

需要使用的时候就直接使用errorText这个资源id就行了
样式树,当我想基于上面的errorText样式来创建一个子样式时可以如下:ger继承了errorText的样式,并新增了textStyle属性.
不过,上述方式不适用与继承Android提供的样式,如果要继承Android提供的样式,需要使用parent属性,如下所示:
<?xml version="1.0" encoding="utf-8"?> <resources>     <style name="CustomTextAppearence" parent="@android:style/TextAppearance">         <item ......./>     </style> </resources>



Theme
Theme实际上就是Style的广泛应用.
如果要制定Theme需要在AndroidManifest.xml中设置,如下:
<activity android:theme="@style/MyTheme"/><application android:theme="@style/MyTheme"/><application android:theme="@android:style/Theme.NoTitleBar"/>

  相关解决方案