您的位置:首页 > 移动开发 > Android开发

android -详谈Style和Theme

2016-08-05 15:31 585 查看
Android上的Style分为了两个方面:

1.Theme是针对窗体级别的,改变窗体样式

2.Style是针对窗体元素级别的,改变指定控件或者Layout的样式。

Android系统的themes.xml和style.xml(位于系统源代码frameworks\base\core\res\res\values\)包含了很多系统定义好的style

一些系统的Theme和Style 给出以下我学习的链接借鉴

http://www.cnblogs.com/qianxudetianxia/p/3725466.html

http://www.cnblogs.com/qianxudetianxia/p/3996020.html

如何新建自定义的风格和主题:

1.在res/values 目录下新建一个名叫style.xml的文件。增加一个<resources>根节点。

2.对每一个风格和主题,给<style>element增加一个全局唯一的名字,也可以选择增加一个父类属性。在后边我们可以用这个名字来应用风格,而父类属性标识了当前风格是继承于哪个风格。

3.在<style>元素内部,申明一个或者多个<item>,每一个<item>定义了一个名字属性,并且在元素内部定义了这个风格的值。

4.你可以应用在其他XML定义的资源。

风格
申明一个风格的实例:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Custom_Text" parent="@style/Text">
<item name="android:textSize">18sp</item>
<item name="android:textColor">#008</item>
</style>
</resources>
引用风格
<EditText id="@+id/text1"
style="@style/Custom_Text<span style="font-family: Arial;">"</span>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="引用了风格Custom_Text" />

主题
申明一个主题的实例:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Custom_Theme">
<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>
引用主题(主题是不能应用在某一个单独的View里)

1.在manifest当中设置主题

<application android:theme="@style/Custom_Theme">//应用全部
<activity android:theme="@android:style/Theme.Dialog">//应用某个activity,用对话框主题来让你的Activity看起来像一个对话框。

2.在程序当中设置主题
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
setTheme(android.R.style.Theme_Light);//用setTheme();方法 要在setContentView();之前
setContentView(R.layout.linear_layout_3);
}
其他
   android:theme="@android:style/Theme.Dialog"   将一个Activity显示为对话框模式
   android:theme="@android:style/Theme.NoTitleBar"  不显示应用程序标题栏
   android:theme="@android:style/Theme.NoTitleBar.Fullscreen"  不显示应用程序标题栏,并全屏
   android:theme="@android:style/Theme.Light"  背景为白色
   android:theme="@android:style/Theme.Light.NoTitleBar"  白色背景并无标题栏 
   android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"  白色背景,无标题栏,全屏
   android:theme="@android:style/Theme.Black"  背景黑色
   android:theme="@android:style/Theme.Black.NoTitleBar"  黑色背景并无标题栏
   android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"    黑色背景,无标题栏,全屏
   android:theme="@android:style/Theme.Wallpaper"  用系统桌面为应用程序背景
   android:theme="@android:style/Theme.Wallpaper.NoTitleBar"  用系统桌面为应用程序背景,且无标题
4000
栏
   android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen"  用系统桌面为应用程序背景,无标题栏,全屏
   android:theme="@android:style/Translucent"
   android:theme="@android:style/Theme.Translucent.NoTitleBar" 半透明,无标题
   android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" 半透明,无标题,全屏
   android:theme="@android:style/Theme.Panel" 面板风格显示
   android:theme="@android:style/Theme.Light.Panel" 平板风格显示
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android Style Theme 布局