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 ...
随机推荐
- openresty开发系列32--openresty执行流程之1初始化阶段
openresty开发系列32--openresty执行流程之初始化阶段 一)初始化阶段 1)init_by_lua init_by_lua_block init_by_lua_file语 ...
- Hive Authorization
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Authorization https://www.cloudera.c ...
- 有相关性就有因果关系吗,教你玩转孟德尔随机化分析(mendelian randomization )
流行病学研究常见的分析就是相关性分析了. 相关性分析某种程度上可以为我们提供一些研究思路,比如缺乏元素A与某种癌症相关,那么我们可以通过补充元素A来减少患癌率.这个结论的大前提是缺乏元素A会导致这种癌 ...
- Ubuntu新建用户并指定目录
例如我要新建一个nginx用户,并指定目录,允许使用bash登录 sudo useradd -d "/home/nginx" -m -s "/bin/bash" ...
- MongoDB的局域网连接问题
问题前两天在本机连接虚拟机的MongoDB,总是连接拒绝 上网百度了一堆,找到一些看似解释,实则不一定管用的玩意. 自己找到一个解法是改etc/mongodb.conf文件,把bindIp的127.0 ...
- 多核vs多处理器
多核vs多处理器 多核CPU性能最好,但成本最高:多CPU成本小,便宜,但性能相对较差 线程数=cpu处理器个数 * 一个cpu内的核数[如果有超线程,再乘以超线程数] 多核 CPU 和多个 CPU ...
- python:封装连接数据库方法
config.py # 数据库测试环境 name = '***' password = '******' host_port_sid = '10.**.*.**:1521/bidbuat' Oracl ...
- python:pytest优秀博客
上海悠悠:https://www.cnblogs.com/yoyoketang/tag/pytest/
- 使用Centos7.5+Nginx+Gunicorn+Django+Python3部署blog项目
项目开发环境是 Python3.5.2+Django1.10.6+Sqlite3+Centos7.5+Nginx1.12.2+Gunicorn 发布出来供需要的同学借鉴参考.文中如有错误请多多指正! ...
- Linux交换分区内存优化
swappiness的值的大小对如何使用swap分区是有着很大的联系的.swappiness=0的时候表示最大限度使用物理内存,然后才是 swap空间,swappiness=100的时候表示积极的使用 ...