当前位置: 首页 > news >正文

Android 样式和主题(style theme)

Android 样式

android中的样式和CSS样式作用相似,都是用于为界面元素定义显示风格,它是一个包含一个或者多个view控件属性的集合。如:需要定义字体的颜色和大小。

在CSS中是这样定义的:

<style>
.wu{COLOR:#0000CC;font-size:18px;}
</style>

可以像这样使用上面的css样式:<div class="wu">wuyudong‘blog</div>

在Android中可以这样定义样式:

在res/values/styles.xml文件中添加以下内容

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name=“wu”> <!-- 为样式定义一个全局唯一的名字-->
<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/wu"
..... />
</LinearLayout>

下面来实践一下

在style.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="text_content_style" parent="AppBaseTheme">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">#66ff00</item>
        <item name="android:textSize">20sp</item>
    </style>

</resources>

布局代码如下:

<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"
    tools:context=".MainActivity" >

    <TextView
        style="@style/text_content_style"
        android:text="我是一个文本" />
    <TextView
        style="@style/text_content_style"
        android:text="我是一个文本" />
    <TextView
        style="@style/text_content_style"
        android:text="我是一个文本" />
    <TextView
        style="@style/text_content_style"
        android:text="我是一个文本" />
    <TextView
        style="@style/text_content_style"
        android:text="我是一个文本" />

</LinearLayout>

运行项目后

Android 主题

android中主题也是用于为应用定义显示风格,它的定义和样式的定义相同,如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name=“wuTheme">
<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/wuTheme">
......
</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看起来像一个对话框,还有透明主题:@android:style/Theme.Translucent 。如果需要查阅这些主题,可以在文档的referenceandroid-->R.style 中查看。

相关文章:

  • Linux作业7
  • 判断终端是ios还是安卓的一些妙用(附加微信分享图标修改)
  • 4: Accessing Environment Variables(Working with programs)
  • flat network 原理与配置 - 每天5分钟玩转 OpenStack(86)
  • 4 张 GIF 图帮助你理解二叉查找树
  • linux目录结构详细介绍
  • Android EditText光标位置(定位到最后)
  • 深刻理解Python中的元类(metaclass)
  • c++中类对象的内存对齐
  • Significance Testing
  • 延迟脚本的方式
  • shell中变量的查看和删除
  • 算法分析-分治 归并排序,递归插入排序,二分查找
  • mysql搭建及数据迁移教程
  • mysql 5.7 zip 文件在 windows下的安装
  • [译]Python中的类属性与实例属性的区别
  • “大数据应用场景”之隔壁老王(连载四)
  • 2018天猫双11|这就是阿里云!不止有新技术,更有温暖的社会力量
  • extjs4学习之配置
  • JavaScript 基础知识 - 入门篇(一)
  • Java多态
  • REST架构的思考
  • SpiderData 2019年2月25日 DApp数据排行榜
  • Spring Cloud(3) - 服务治理: Spring Cloud Eureka
  • 技术胖1-4季视频复习— (看视频笔记)
  • 思考 CSS 架构
  • 我建了一个叫Hello World的项目
  • 吴恩达Deep Learning课程练习题参考答案——R语言版
  • 用element的upload组件实现多图片上传和压缩
  • 运行时添加log4j2的appender
  • ​创新驱动,边缘计算领袖:亚马逊云科技海外服务器服务再进化
  • ​水经微图Web1.5.0版即将上线
  • #设计模式#4.6 Flyweight(享元) 对象结构型模式
  • (6)【Python/机器学习/深度学习】Machine-Learning模型与算法应用—使用Adaboost建模及工作环境下的数据分析整理
  • (delphi11最新学习资料) Object Pascal 学习笔记---第8章第5节(封闭类和Final方法)
  • (附源码)spring boot车辆管理系统 毕业设计 031034
  • (附源码)SSM环卫人员管理平台 计算机毕设36412
  • (更新)A股上市公司华证ESG评级得分稳健性校验ESG得分年均值中位数(2009-2023年.12)
  • (过滤器)Filter和(监听器)listener
  • (理论篇)httpmoudle和httphandler一览
  • (淘宝无限适配)手机端rem布局详解(转载非原创)
  • (五)关系数据库标准语言SQL
  • (转)GCC在C语言中内嵌汇编 asm __volatile__
  • .bat批处理(七):PC端从手机内复制文件到本地
  • .NET Core 将实体类转换为 SQL(ORM 映射)
  • .NET 动态调用WebService + WSE + UsernameToken
  • @Autowired和@Resource的区别
  • @ComponentScan比较
  • @DateTimeFormat 和 @JsonFormat 注解详解
  • @DependsOn:解析 Spring 中的依赖关系之艺术
  • @Service注解让spring找到你的Service bean
  • @zabbix数据库历史与趋势数据占用优化(mysql存储查询)
  • [ 数据结构 - C++]红黑树RBTree
  • [30期] 我的学习方法
  • [AIGC] Spring Interceptor 拦截器详解