首先,我们来确定下style和Theme都是做什么用的?
我们都知道,我们可以通过在main.xml或者其他的布局文件中通过属性设置来设置控件的样式,这就导致了我们基本上要为每一个控件都一一的设置属性,更让人恼火的是,这些属性里面的很多都是相同的,欲哭无泪啊!
在这个程序员天天期待救世主出现的年代,天空一声惊雷,style和Theme凭空降临了,帮我们解决了这个天大的难题。style允许我们可以将一些公共的属性设置放到一起,然后定义一个style名称,我们在控件中想使用这些设置的时候,只需要调用下这个style就可以了。至于Theme,你可以认为它是一种特殊的style。
1)、style一般用来控制控件的属性控制
2)、Theme一般是用来控制窗口的属性设置
--------------------------------------------------------------------
下面我们就一起来介绍下,具体的应用办法:
问题一:我们的style设置都放在那里呢?
我们的style(包括Theme)都存放在res/values/*.xml中,这里的xml文件名称我们可以随意,但是为了规范,我们一般都使用style.xml或者styles.xml
style代码示例:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<style name="TextView">
<item name="android:textSize">18sp</item>
<item name="android:textColor">#008</item>
<item name="android:shadowColor">@android:color/black</item>
<item name="android:shadowRadius">2.0</item>
</style>
<style name="EditText">
<item name="android:shadowColor">@android:color/black</item>
<item name="android:shadowRadius">1.0</item>
<item name="android:background">@android:drawable/btn_default</item>
<item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
</style>
<style name="Button">
<item name="android:background">@android:drawable/edit_text</item>
<item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
</style>
</resources>
xml中我们定义了三个style,当我们在程序中调用的时候,便可以通过style的name属性值来调用。(注意:我们说过了style.xml的名称可以随意定义,但是里面标签的结构却不是随意的,想定义style,则必须在resources标签下定义style标签,内容的子属性通过item来进行设置)
我们再来看看一个Theme的示例:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme" parent="android:Theme.Light">
<item name="android:windowFullscreen">true</item>
<item name="android:windowTitleSize">160dip</item>
<item name="android:windowTitleStyle">@style/WindowTitle</item>
</style>
<style name="WindowTitle" parent="android:WindowTitle">
<item name="android:singleLine">true</item>
<item name="android:shadowColor">#BB000000</item>
<item name="android:shadowRadius">5.75</item>
</style>
</resources>
大家发现了没有,一模一样的结构!这也是我刚才为什么说,其实Theme就是一种特殊style的原因。
-----------------------------------------------------------------
我们再来看看如何调用我们定义好的style。这里假设,我们有一个和EditText相关的style,该style的name属性为style_editText,有一个和窗口设置相关的Theme,该Theme的名称为theme_ok。那么我们如何使用它们呢?
第一种:为某个特定的EditText使用style_editText。
<EditText
android:id="@+id/myEditText"
android:layout_width="fill_parent"
android:layout_height="wrap+parent"
style="@style/style_editText"
android:hit="欢迎来到chenzheng_java博客"
/>
第二种:在程序中使用主题theme_ok
- public class ThemeStyleActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setTheme(R.style.theme_ok);
- setContentView(R.layout.main);
- }
- }
我们可以看到,我们通过setTheme方法来实现的。
第三种:为整个应用或者某个activity添加主题
为整个应用添加主题,我们可以在AndroidManifest.xml中通过为<application>标签添加实行android:theme属性来实现。例如android:theme="@style/Theme_ok"
为activity添加主题,我们可以在AndroidManifest.xml中通过为<activity>标签添加实行android:theme属性来实现。例如android:theme="@style/Theme_ok"
---------------------------------------------------------------------------