Android样式——Styles
Android 样式 styles
2023-09-11 14:20:18 时间
https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/styles.xml (android默认样式)
Android样式——Styles
说明
样式(style)是属性的集合,用来指定View或者Window的外观和格式。
这些属性可以是height(高度)、padding(内边距)、font size(字体颜色)等。
样式定义在另一个xml文件中,从布局文件中分离出来。
例如:
1 <TextView 2 android:layout_width="fill_parent" 3 android:layout_height="wrap_content" 4 android:textColor="#00FF00" 5 android:typeface="monospace" 6 android:text="@string/hello" />
如果使用样式,则可以转换为
1 <TextView 2 style="@style/CodeFont" 3 android:text="@string/hello" />
CodeFont就是样式文件名,把一些属性从原布局文件中提取出来,存放到了CodeFont文件中。
定义
样式文件必须存放在res/valuse的文件夹中,命名任意,后续为.xml。
CodeFont.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 <style name="CodeFont" parent="@style/BaseFont"> 4 <item name="android:layout_width">fill_parent</item> 5 <item name="android:layout_height">wrap_content</item> 6 <item name="android:textColor">#00FF00</item> 7 <item name="android:typeface">monospace</item> 8 </style> 9 </resources>
<resources>:根元素。
<style>:属性集合。
<item>:属性。
使用parent,可以继承样式。除此之外,还可以在<style>的命名前添加"继承样式名.",效果一样。例如:
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="BaseFont.CodeFont" > <item name="android:layout_width">fill_parent</item> <item name="android:layout_height">wrap_content</item> <item name="android:textColor">#00FF00</item> <item name="android:typeface">monospace</item> </style> </resources>
注意:
引用此样式时的名字为BaseFont.CodeFont。
如果引用系统自带的样式,值的格式为“@android:style/样式名”。我们自定义的样式,值的格式为“@style/样式名”。
应用
两种方式
对单独的View,举例:
布局文件中,<TextView style="@style/CodeFont" android:text="@string/hello" />
对整个Activity或整个应用程序,举例:
AndroidManifest.xml文件中,<activity android:theme="@android:style/Theme.Dialog">或者<application android:theme="@style/CustomTheme">
相关文章
- [Android] 解析android framework下利用app_process来调用java写的命令及示例
- [Android Pro] android Flag介绍
- [Android Pro] Android权限设置android.permission完整列表
- [Android Memory] android 警告:Exported activity does not require permission
- Android中如何使用自定义对话框
- Android spinner 样式及其使用详解
- 《Android进阶之光》--注解与依赖注入框架
- Android开发学习---使用Intelij idea 13.1 进行android 开发
- 《Android虚拟机》----虚拟机概述
- [Android]ListView性能优化之视图缓存
- Android开发指南(40) —— Adding Recent Query Suggestions
- Android【报错】. lang。android.app ClassCastException。SharedPreferencesImpl不能被强制转换为android.content.Shared
- Android【报错】This class should be public (android.support.v7.internal.widget.ActionBarView.HomeView)
- Android 10.0 Launcher3去掉抽屉模式 双层改成单层系列一
- Android 10.0 framework 增加音量+音量-键唤醒屏幕的功能
- Android 10.0 SystemUI修改状态栏电池图标样式为横屏显示
- android 10.0 更换壁纸加载慢滑动卡顿的解决
- 【Android 插件化】Hook 插件化框架 ( 合并 “插件包“ 与 “宿主“ 中的 Element[] dexElements | 设置合并后的 Element[] 数组 )
- 【Android 安装包优化】7z 文件压缩格式 ( 7z 格式简介 | 7z 命令使用说明 )
- Android的Bitmap和BitmapDrawable类解析-android学习之旅(六十)
- 2014 android毕设代做 代做Android毕设 安卓毕设
- android:更改PagerTabStrip背景颜色,标题字体样式、颜色和图标,以及指示条的颜色
- Android有关JNI 学习(两)为JNI方法名称,数据类型和方法签名的一些知识
- TeamViewer安装 windows linux MacOS chrome android ios
- Win10 WSL编译Android开启ccache(提高编译速度)
- 【Android架构GPS篇】之GPS定位应用层流程
- Android JNI学习-线程相关操作
- Android studio之Button点击事件的四种实现方法