Android笔记(七十二) Style和Theme
我们尝尝需要使用setText、setColor、setTextSize等属性来设置控件的样式,但是每个控件都需要设置这些属性,工作量无疑是巨大的,并且后期维护起来也不方便。
Style
Android中的样式(style)包含一组格式,为一个组件设置使用某个样式时,该样式所包含的全部格式都将会应用在这个组件上。
Android的样式资源文件放在/res/values目录下,其跟元素是<resources>,该元素内包含多个<style>子元素,每个子元素都是一个样式。
<style>元素指定以下两个属性:
1.name:指定样式名称
2.parent:指定该样式所继承的父样式
每个<style>元素中包含多个<item>子元素,每个<item>子元素定义一个格式。
/res/values/styles.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="TextView">
<item name="android:background">#00ff00</item>
<item name="android:textSize">30dp</item>
<item name="android:textColor">#ffff00</item>
</style>
<style name="TextView2" parent="@style/TextView">
<item name="android:textColor">#0f0f0f</item>
<item name="android:layout_marginTop">10dp</item>
</style>
</resources>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.styleandtheme.MainActivity" > <TextView
style="@style/TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is a Text" /> <TextView
style="@style/TextView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is a Text" /> </LinearLayout>
运行结果:

Theme
Theme和Style非常相似,同样是定义在values目录下,同样以<resource>作为根元素,同样以<style>定义主题,主题中同样包含<item>每个item表示一个样式
和Style不同的是Theme不能作用于单个的组件,它是对整个Activity起作用的,Theme定义的格式是改变窗口的外观,例如窗口标题,窗口边框等等
/res/values/styles.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppBaseTheme" parent="android:Theme.Light"></style>
<style name="AppTheme" parent="AppBaseTheme"></style>
<style name="TextView">
<item name="android:background">#00ff00</item>
<item name="android:textSize">30dp</item>
<item name="android:textColor">#ffff00</item>
</style>
<style name="TextView2" parent="@style/TextView">
<item name="android:textColor">#0f0f0f</item>
<item name="android:layout_marginTop">10dp</item>
</style>
<style name="MyTheme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
</resources>
MainActivity.java
package com.example.styleandtheme; import android.app.Activity;
import android.os.Bundle; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.MyTheme);
setContentView(R.layout.activity_main);
}
}
运行结果:

Android笔记(七十二) Style和Theme的更多相关文章
- Android笔记(十二)AndroidManiFest.xml
AndroidManiFest.xml清单文件是每个Android项目所必须的,它是整个Android应用的全局描述文件.AndroidManiFest.xml清单文件说明了该应用的名称.所使用的图标 ...
- Android笔记(七十六) 点菜DEMO
一个朋友让看一下他的代码,一个点菜的功能,他和我一样,初学者,代码比我的都混乱,也是醉了,干脆想着自己写个demo给他看,原本想着听简单,半个小时应该就可以搞定,真正写的时候,画了3h+,汗颜... ...
- Android笔记(七十五) Android中的图片压缩
这几天在做图记的时候遇第一次遇到了OOM,好激动~~ 追究原因,是因为在ListView中加载的图片太大造成的,因为我使用的都是手机相机直接拍摄的照片,图片都比较大,所以在加载的时候会出现内存溢出,那 ...
- Android笔记(七十四) 详解Intent
我们最常使用Intent来实现Activity之间的转跳,最近做一个app用到从系统搜索图片的功能,使用到了intent的 setType 方法和 setAction 方法,网上搜索一番,发现实现转跳 ...
- Android笔记(七十) AlertDialog
alertdialog可以在当前界面中弹出一个对话框,这个对话框在界面所有元素之上,可以屏蔽掉其他控件的交互能力,因此alertdialog常用于一些重要的内容警告. 使用AlertDialog.Bu ...
- Android笔记(六十二)网络框架volley
什么是Volley 很多时候,我们的APP都需要用到网络技术,使用HTTP协议来发送接收数据,谷歌推出了一个网络框架——volley,该框架适合进行数据量不大,但通信频繁的网络操作. 它的优点: (1 ...
- Android群英传笔记——第十二章:Android5.X 新特性详解,Material Design UI的新体验
Android群英传笔记--第十二章:Android5.X 新特性详解,Material Design UI的新体验 第十一章为什么不写,因为我很早之前就已经写过了,有需要的可以去看 Android高 ...
- VSTO 学习笔记(十二)自定义公式与Ribbon
原文:VSTO 学习笔记(十二)自定义公式与Ribbon 这几天工作中在开发一个Excel插件,包含自定义公式,根据条件从数据库中查询结果.这次我们来做一个简单的测试,达到类似的目的. 即在Excel ...
- 深度学习课程笔记(十二) Matrix Capsule
深度学习课程笔记(十二) Matrix Capsule with EM Routing 2018-02-02 21:21:09 Paper: https://openreview.net/pdf ...
随机推荐
- matlab学习笔记7-定时器
一起来学matlab-matlab学习笔记7-定时器 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 <matlab 程序设计与综合应用>张德丰等著 感谢张老师的书籍,让 ...
- SAP 更新模块1
RSM13000 / RSM13000 / 5.747FORM / VB_CALL_FUNC CALL 'ThVBCall' ID 'OPCODE' FIELD vb_update_modul_pro ...
- WebAPI.请求(Request)的参数(Parameter)里包含特殊字符(#等)的正确处理方式 从客户端xx中检测到有潜在危险的Request.Form值
事故现场 报错"从客户端 中检测到有潜在危险的Request.Form值" //后台代码 namespace Test { [RoutePrefix("TestClass ...
- bladex下载前端代码后,运行服务时报错【'vue-cli-service' 不是内部或外部命令,也不是可运行的程序或批处理文件。】的解决方法
问题:E:\BladeXDB\Saber>yarn run serveyarn run v1.13.0$ vue-cli-service serve'vue-cli-service' 不是内部或 ...
- LinQ中合并、连接、相交、与非查询
LinQ中Union合并查询:连接不同的集合,自动过滤相同项:延迟.即是将两个集合进行合并操作,过滤相同的项 var cities = (from p in mylinq.System_Places ...
- 【Python学习之十一】Numpy
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 python3.6 1.介绍NumPy(Numerical Pyt ...
- 「中山纪中集训省选组D2T1」书堆 欧拉常数
题目描述 蚂蚁是勤劳的动物,他们喜欢挑战极限.现在他们迎来了一个难题!蚂蚁居住在图书馆里,图书馆里有大量的书籍.书是形状大小质量都一样的矩形.蚂蚁要把这些书摆在水平桌子的边缘.蚂蚁喜欢整洁的布置,所以 ...
- war包方式部署solo博客
solo,一款小而美的博客系统,GitHub:https://github.com/b3log/solo 环境和文件准备 服务器:用的阿里云服务器,系统是 CentOS 7.3 64 位. JDK:1 ...
- jstack的使用:死锁问题实战
- gitlab-runner 安装使用
gitlab-runner 安装使用 gitlab-runner 是一个开源的与 gitlab CI 配合使用的项目,用于运行任务,并将结果返回 gitlab 本文通过docker in docker ...