巧用style的另类写法
看到style,不少人可能会说这个我知道,就是控件写属性的话可以通过style来实现代码的复用,单独把这些属性及其参数写成style就可以便捷的调用。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomText" parent="@style/Text">
<item name="android:textSize">20sp</item>
<item name="android:textColor">#008</item>
</style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<EditText
style="@style/CustomText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello, World!" />
这种写法呢其实比较常见,如果有某些控件用到了相同的风格,就可以用style来作,今天要讲的不是这种写法,下面先看一下案例
<EditText id="text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="?android:textColorSecondary"
android:text="@string/hello_world" />
请注意其中的 android:textColor="?android:textColorSecondary" ,这里给的是一个reference,而且是系统的资源。官网上有写介绍http://developer.android.com/guide/topics/resources/accessing-resources.html
这种写法叫做“Referencing style attributes”即引用风格属性,那么怎么引用自定义的东西呢,是首先需要定义attributes,这和自定义控件时写的style声明是一样的,遵照下面的格式定义一个activatableItemBackground的属性,值得类型是reference引用类型,
<resources>
<declare-styleable name="TestTheme">
<attr name="activatableItemBackground" format="reference"></attr>
</declare-styleable>
</resources>
接着定义我们的theme,并且把这个属性用上去,theme实际上也是style,只是针对Application和Activity时的不同说法而已。
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="activatableItemBackground">@drawable/item_background</item>
</style>
</resources>
theme写好后我们把它应用到application或者某个activity,
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.avenwu.sectionlist.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
这些准备工作完成后,恭喜你现在可以用 ?[<package_name>:][<resource_type>/]<resource_name>的方式来写布局了,注意开头是?不是@。
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView"
android:gravity="center"
android:background="?activatableItemBackground"
android:clickable="true"
android:layout_gravity="left|top"/>
这个TextView用两个?,一个引用系统资源,background则用了我们刚刚准备的资源。
最后我们来讲一下,为什么用这种方式,实际上这种写法主要是剥离了具体的属性值,好比我们的background,现在对应的值不是一个确定的值,它依赖于theme里面的具体赋值,这样的话如果需要更换主题风格我们就不需要针对每个布局改来改去,只要重新写一个对应不同资源的theme就可以了。当然即使不用做换肤也完全没问题,这里只是提供另一个思路来写布局的属性。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?