Android Style.xml 详解
来源:互联网 发布:中兴汽车怎么样 知乎 编辑:程序博客网 时间:2024/05/16 10:07
概述
根据 Android 官方的解释
A style resource defines the format and look for a UI. A style can be applied to an individual View (from within a layout file) or to an entire Activity or application (from within the manifest file).
style 定义的UI格式和外观,它可以单独被一个 View 使用,也可以给Activity使用或者作为 Application 风格(前者在 layout中使用 android:style=”XXXX” 后者在 manifest 文件的 application 节点下 使用 android:theme=”XXXX” )。
其实总的来说, style 就是一组属性集合,例如 width ,padding,color,height,等,更复杂一点的包括 windowNoTitle,windowBackground,windowContentOverlay 等高级属性。
其实 style 和 web 开发的 stylesheets 很类似,是一种把内容和样式分离的思想,这种单纯的编程思想有助于我们将问题剥离能够更好的帮助我们分析问题,另外一点就是,提高了代码的复用率(似乎这个是为了省力),同时也让我们的代码看起来更干净,层次分明呀。
tip:style 的引用根据的是 sytle 的name 属性,而不是xml的文件名称。
例如:
style 中的代码
<style name="cart_but"> <item name="android:shape">@drawable/shape_cart_but</item> <item name="android:layout_height">25dp</item> <item name="android:layout_width">45dp</item> <item name="android:layout_gravity">center_vertical</item> <item name="android:textSize">22sp</item> <item name="android:textStyle">bold</item></style>
layout 中的使用
<Button android:id="@+id/but_cart_reduce" style="@style/cart_but" android:text="-" />
实际上,resources 下的每一个 style 节点在编译的时候都会被编译成一个 application resource 对象,
常见用法
单独使用在一个 View 上
<Button android:id="@+id/but_cart_reduce" style="@style/cart_but" android:text="-" />
只对当前 View 起作用,即使当前 View 是一个 ViewGroup 它也只是对这个 ViewGroup 起作用,而不会影响其中的 child View 。
使用在 Activity 或者 Application 上,这样的话是影响了所有 Application 下的 UI 格式和风格
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > </application>
关于 style 的继承
style 允许继承,你可以在子 style 中重写父 style 的属性,从而提高了灵活程度,下面是一个 example,通过 parent 属性,实现继承,子类命名和父类命名存在一定关系,子类命名为:父类命名.扩展名称。如果是自定义的 style 可以通过命名继承来实现继承,子类名称为 父类名称.扩展名称。
<style name="cart_tv"> <item name="android:layout_height">wrap_content</item> <item name="android:layout_width">match_parent</item> <item name="android:paddingLeft">2dp</item> <item name="android:layout_gravity">left</item> <item name="android:textColor">#f25</item></style><style name="cart_tv.brand"> <item name="android:textSize">18sp</item> <item name="android:minEms">2</item> <item name="android:ellipsize">end</item> <item name="android:layout_marginTop">20dp</item></style>
style cart_tv.brand 是继承自 cart_tv 的,相同属性子类会覆盖父类的值。
tip:这种继承命名,不能使用在系统自带的 style上,你需要显示的使用 parent 属性,进行声明,例如:
<style name="CartDialog" parent="android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@drawable/shape_bg_dialog</item> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item></style>
parent=”android:style/Theme.Dialog” 声明了这个 style 继承自系统的 Theme.Dialog
如何使用系统的 Style 和 Theme
系统的 style 在 R.style 静态类中,你可以在 xml 文件中使用以下类似代码,实现引用
"@android:style/Theme.NoTitleBar".
也就是 @android:style/你要引用的属性名称
- Android Style.xml 详解
- Android Style XML
- Android中Style详解
- Android:Style详解
- android开发style详解
- android style.xml的使用
- android开发xml style使用
- android attr.xml my style
- android中样式使用style.xml
- android xml文件重复控件 包装style
- Android入门--style.xml设置和应用
- Android资源文件夹下面values/style.xml、values-v19/style.xml、values-v21/style.xml主题调用规则
- android 中theme.xml与style.xml的区别
- android 中theme.xml与style.xml的区别
- android 中theme.xml与style.xml使用总结
- android 笔记style与theme详解
- Android中的Attr、Style和Theme详解
- style详解
- ubuntu运行android studio出错unable to run mksdcard sdk tool
- ffmpeg 视频
- .net中的System.Threading.Interlocked类可以为多个线程共享的变量提供原子操作
- 代理模式
- 浅谈HTTP中Get与Post的区别
- Android Style.xml 详解
- Spanned.SPAN_EXCLUSIVE_EXCLUSIVE的含义
- 【软件设计模式】代理模式
- Spring3.0
- 我的第一个Struts2框架开发-Hello World
- 关于MFC客户端窗口全屏和撤销全屏问题
- OkHttp使用方法详解
- 文档中导入 UE编辑器
- 三 java解析xml文件