如何新建自定义的style和theme:
1.在res/values 目录下新建一个名叫style.xml的文件。增加一个<resources>根节点。
2.对每一个style和theme,给<style>element增加一个全局唯一的名字,也可以选择增加一个父类属性。(继承)
3.在<style>元素内部,申明一个或者多个<item>,每一个<item>定义了一个名字属性,并且在元素内部定义了这个风格的值。
4.你可以应用在其他XML定义的资源。
Style:
下边是一个申明风格的实例:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="SpecialText" parent="@style/Text">//继承父类的属性
<item name="android:textSize">18sp</item>
<item name="android:textColor">#008</item>
</style>
</resources>
然后可以在控件中去引用
Theme:
就像style一样,主题依然在<style>元素里边申明,也是以同样的方式引用。窗体级别的不同的是你通过在Android Manifest中定义的<application>和<activity>元素将主题添加到整个程序或者某个 Activity,但是主题是不能应用在某一个单独的View里。下边是申明主题的一个例子:<?xml version="1.0" encoding="utf-8"?> <resources> <style name="CustomTheme"> <item name="android:windowNoTitle">true</item> <item name="windowFrame">@drawable/screen_frame</item> <item name="windowBackground">@drawable/screen_background_white</item>//引用已经存在或系统定义的 <item name="panelForegroundColor">#FF000000</item> <item name="panelBackgroundColor">#FFFFFFFF</item> <item name="panelTextColor">?panelForegroundColor</item> <item name="panelTextSize">14</item> <item name="menuItemTextColor">?panelTextColor</item>//引用自己定义的 <item name="menuItemTextSize">?panelTextSize</item> </style> </resources>注意我们用了@符号和?符号来应用资源。@符号表明了我们应用的资源是前边定义过的(或者在前一个项目中或者在Android 框架中)。问号?表明了我们引用的资源的值在当前的主题当中定义过。1.在manifest当中设置主题
application中设置
<application android:theme="@style/CustomTheme">2.如果你只是想让你程序当中的 某个Activity拥有这个主题,那么你可以 编辑<activity>标签。Android中提供了几种内置的资源,有好几种主题你可以切换而不用自己写。比如你可以用对话框主题来让你的Activity看起来像一个对话框。在manifest中定义如下: <activity android:theme="@android:style/Theme.Dialog"><style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">//继承父主题
把第二个activity设置透明主题的话 跳转后第二个activity不会调用onstop方法
<style name="Theme.Translucent">5: <item name="android:windowBackground">@android:color/transparent</item>6: <item name="android:colorBackgroundCacheHint">@null</item>7: <item name="android:windowIsTranslucent">true</item>11: <item name="android:windowAnimationStyle">@android:style/Animation</item>12: </style>2.在代码当中设置主题
setTheme(android.R.style.Theme_Light);//必须在显示ui控件之前设置 setContentView(R.layout.linear_layout_3);
跳转后第一个activity不会调用onstop方法