我们尝尝需要使用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的更多相关文章

  1. Android笔记(十二)AndroidManiFest.xml

    AndroidManiFest.xml清单文件是每个Android项目所必须的,它是整个Android应用的全局描述文件.AndroidManiFest.xml清单文件说明了该应用的名称.所使用的图标 ...

  2. Android笔记(七十六) 点菜DEMO

    一个朋友让看一下他的代码,一个点菜的功能,他和我一样,初学者,代码比我的都混乱,也是醉了,干脆想着自己写个demo给他看,原本想着听简单,半个小时应该就可以搞定,真正写的时候,画了3h+,汗颜... ...

  3. Android笔记(七十五) Android中的图片压缩

    这几天在做图记的时候遇第一次遇到了OOM,好激动~~ 追究原因,是因为在ListView中加载的图片太大造成的,因为我使用的都是手机相机直接拍摄的照片,图片都比较大,所以在加载的时候会出现内存溢出,那 ...

  4. Android笔记(七十四) 详解Intent

    我们最常使用Intent来实现Activity之间的转跳,最近做一个app用到从系统搜索图片的功能,使用到了intent的 setType 方法和 setAction 方法,网上搜索一番,发现实现转跳 ...

  5. Android笔记(七十) AlertDialog

    alertdialog可以在当前界面中弹出一个对话框,这个对话框在界面所有元素之上,可以屏蔽掉其他控件的交互能力,因此alertdialog常用于一些重要的内容警告. 使用AlertDialog.Bu ...

  6. Android笔记(六十二)网络框架volley

    什么是Volley 很多时候,我们的APP都需要用到网络技术,使用HTTP协议来发送接收数据,谷歌推出了一个网络框架——volley,该框架适合进行数据量不大,但通信频繁的网络操作. 它的优点: (1 ...

  7. Android群英传笔记——第十二章:Android5.X 新特性详解,Material Design UI的新体验

    Android群英传笔记--第十二章:Android5.X 新特性详解,Material Design UI的新体验 第十一章为什么不写,因为我很早之前就已经写过了,有需要的可以去看 Android高 ...

  8. VSTO 学习笔记(十二)自定义公式与Ribbon

    原文:VSTO 学习笔记(十二)自定义公式与Ribbon 这几天工作中在开发一个Excel插件,包含自定义公式,根据条件从数据库中查询结果.这次我们来做一个简单的测试,达到类似的目的. 即在Excel ...

  9. 深度学习课程笔记(十二) Matrix Capsule

    深度学习课程笔记(十二) Matrix Capsule with EM Routing  2018-02-02  21:21:09  Paper: https://openreview.net/pdf ...

随机推荐

  1. Amazon | OA 2019 | Optimal Utilization

    Given 2 lists a and b. Each element is a pair of integers where the first integer represents the uni ...

  2. Swift编码总结7

    1.Git 打Tag: 命令也就下面两条,看看就会了. 2.字符串转Model:JSONDecoder http://www.cocoachina.com/ios/20180612/23771.htm ...

  3. Indy10 Tcp接收数据问题

    在做Delphi开发时,使用Indy组件来做网络通讯是一种比较快捷的方式.今天要说一下indy10中tcp接收数据的问题. 我们在测试时经常使用Wrinteln来发送数据,用Readln来接收数据.用 ...

  4. PostgreSql11.5源码安装

    参考:https://yq.aliyun.com/articles/675687/ 1.先下载postgresql11.5的源码包:https://www.postgresql.org/ftp/sou ...

  5. 【python小记】python操作excel文件

    题记: 最近因为工作需要,学习了python,瞬间对这个轻松快捷的语给吸引了,以前只知道js脚本是写网页的,没有想到python这个脚本语言的应用范围可以这么广泛,现在做一些简单或稍微复杂的操作,基本 ...

  6. js玩命加载……

    在请求数据加载的过程中,经常需要显示请求等待,写了一个简单的请求等待—- html代码如下 <!--页面载入显示--> <div id="dataLoad" st ...

  7. [转帖]WannaCry惊天大发现!疑似朝鲜黑客组织Lazarus所为

    WannaCry惊天大发现!疑似朝鲜黑客组织Lazarus所为 Threatbook2017-05-16共588524人围观 ,发现 17 个不明物体系统安全 https://www.freebuf. ...

  8. 20190715《Python网络数据采集》第 1 章

    <Python网络数据采集>7月8号-7月10号,这三天将该书精读一遍,脑海中有了一个爬虫大体框架后,对于后续学习将更加有全局感. 此前,曾试验看视频学习,但是一个视频基本2小时,全部拿下 ...

  9. ColorMatrixFilter色彩矩阵滤镜

    ColorMatrixFilter色彩矩阵滤镜: /** * * *----------------------------------------* * | *** ColorMatrixFilte ...

  10. [LOJ6432] [PKUSC2018] 真实排名

    题目链接 LOJ:https://loj.ac/problem/6432 Solution 假设我们当前要算\(x\)的答案,分两种情况讨论: \(x\)没被翻倍,那么\([a_x/2,a_x]\)这 ...