`
- 浏览:
31661 次
-
android 中的样式和 CSS 样式作用相似,都是用于为界面元素定义显示风格,它是一个包含一个或者多个 view 控件属性的集合。如:需要定义字体的颜色和大小。
在 CSS 中是这样定义的:
<style>
.itcast{COLOR:#0000CC;font-size:18px;}
</style>
可以像这样使用上面的 css 样式: <div class="itcast"> 传智播客 </div>
在 Android 中可以这样定义样式:
在 res/values/styles.xml 文件中添加以下内容
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name=“itcast”> <!-- 为样式定义一个全局唯一的名字 -->
<item name="android:textSize">18px</item> <!-- name 属性为样式要用在的 View 控件持有的属性 -->
<item name="android:textColor">#0000CC</item>
</style>
</resources>
在 layout 文件中可以像下面这样使用上面的 android 样式:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ....>
<TextView style="@style/itcast"
..... />
</LinearLayout>
<style> 元素中有一个 parent 属性。这个属性可以让当前样式继承一个父样式,当前样式可以继承到父样式的值。当然,如果父样式的值不符合你的需求,你也可以对它进行修改,如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="itcast">
<item name="android:textSize">18px</item> <!-- name 属性为样式要用在的 View 控件持有的属性 -->
<item name="android:textColor">#0000CC</item>
</style>
<style name="subitcast" parent="@style/itcast">
<item name="android:textColor">#FF0000</item>
</style>
</resources>
android 中主题也是用于为应用定义显示风格,它的定义和样式的定义相同,如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name=“itcastTheme">
<item name=“android:windowNoTitle”>true</item> <!– 没标题 à
<item name=“android:windowFullscreen”>?android:windowNoTitle</item> <!– 全屏显示 à
</style>
</resources>
上面“ ?android:windowNoTitle” 中的问号用于引用在当前主题中定义过的资源的值。下面代码显示在 AndroidManifest.xml 中如何为应用设置上面定义的主题:
<application android:icon="@drawable/icon" android:label="@string/app_name"
android:theme="@style/itcastTheme">
......
</application>
除了可以在 AndroidManifest.xml 中设置主题,同样也可以在代码中设置主题,如下:
setTheme(R.style.itcastTheme);
尽管在定义上,样式和主题基本相同,但是它们使用的地方不同。样式用在单独的 View ,如: EditText 、 TextView 等;主题通过 AndroidManifest.xml 中的 <application> 和 <activity> 用在整个应用或者某个 Activity ,主题对整个应用或某个 Activity 进行全局性影响。如果一个应用使用了主题,同时应用下的 view 也使用了样式,那么当主题与样式属性发生冲突时,样式的优先级高于主题。
另外 android 系统也定义了一些主题,例如: <activity android:theme=“@android:style/Theme.Dialog”> ,该主题可以让 Activity 看起来像一个对话框
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
android_UI进阶之style和theme的总结,供学习交流,谢谢~
对android中的style和theme进行了比较好的介绍。
Android_样式(style)&主题(theme)&显示单位
发现坑 最近在配置项目主题的时候报了如下错误: This Activity already has an...style name=AppTheme.NoActionBar> <item name=android:windowActionBar>false <item name=android:windowNoTitle>true <ite
NULL 博文链接:https://trinityblood.iteye.com/blog/1743655
android 主题(Theme)实现 setTheme() android2.2
本篇文章是对android中的样式与主题(style&theme)进行了详细的分析介绍,需要的朋友参考下
NULL 博文链接:https://txlong-onz.iteye.com/blog/930222
android:theme=”@android:style/Theme.Dialog” : Activity显示为对话框模式 android:theme=”@android:style/Theme.NoTitleBar” : 不显示应用程序标题栏 android:theme=”@android:style/Theme.NoTitleBar....
android style(样式)和theme(主题)设置
android 运行时动态切换style, theme可以使用于整个application,而style适用于单个的view。
安装 Android SDK 和Eclipse 插件 Android应用 android样式和主题(style&theme) Activity生命周期
style name=Splash_Theme parent=@android:style/Theme.NoTitleBar> </style> 原因是我们给改Activity/Application设置的主题引起的,因为该主题相对应的windowBackground等背景被设置成了白色或者黑色,且...
style name=”AppBaseTheme” parent=”android:Theme.Light”> </style> <style name=”AppTheme” parent=”AppBaseTheme”> </style> <style name=”testStyle”> <item name...
主要介绍了Android入门之Style与Theme用法,非常实用的功能,需要的朋友可以参考下
@android:style/Theme.Translucent @android:style/Theme.Translucent.NoTitleBar @android:style/Theme.Translucent.NoTitleBar.Fullscreen 只需要在Manifest中需要透明的Activity内设置theme为以上任意一个就可以...
Android Theme和Styles内部定义解析.txt
主要介绍了Android Style.xml的应用详解及代码实现的相关资料,需要的朋友可以参考下
详细讲述style和theme的区别和使用方法,非常适合初学者!
在Android SDK中内置了下面的Theme,可以按标题栏Title Bar和状态栏Status Bar是否可见来分类: 代码如下: android:theme=”@android:style/Theme.Dialog” 将一个Activity显示为能话框模式 android:theme=”@...