看到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就可以了。当然即使不用做换肤也完全没问题,这里只是提供另一个思路来写布局的属性。

巧用style的另类写法的更多相关文章

  1. .net EasyTree显示所级层级(无限级、整层级颗树)的另类写法。非递归

    获取整颗树的另类写法.非递归 //获取所有的菜单 List<T_Menu> menu = bll.getMenuByUsesrID("8189a7c1-6f15-4744-b6c ...

  2. js获取元素位置和style的兼容性写法

    今天说一下js获取元素位置和style的方法.当然不只是element.style那么简单.. 主角:getBoundingClientRect,getClientRects,getComputedS ...

  3. javascript另类写法

    今天来介绍一下javascript不一样的写法,很简单哦. 1.当条件成立时执行a方法,当条件失败是执行b方法 通常我们会这样写: var result; if(isOk){ result=funA( ...

  4. addEventListener 的另类写法

    addEventListener 参数如下 addEventListener(type, listener[, useCapture]); type,事件名称 listener,事件处理器 useCa ...

  5. Tip提示框另类写法

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. sql查询条件为空的另类写法o( ̄▽ ̄)d

    简单描述:今天看老大提交的代码,发现了一个有意思的事情,一条sql中判断条件是空,老大的写法,让我眼前一亮.直接上代码 代码: <select id="getxxxs" re ...

  7. 动态class,style,src绑定写法 vue

    :class="{active:menuName===item.title}" :style="thisTitle==='一张图展示'?styles:''" : ...

  8. logstash中output{}的另类写法

    日志传输路径如下: filebeat->redis->logstash->es 在filebeat配置文件中,收集日志的时候配置的有如下参数: fields: log_source: ...

  9. 项目名 的在JSP或JAVA中的另类写法

    在JSP页面中${pageContext.request.contextPath } 表示项目名<form action="${pageContext.request.contextP ...

随机推荐

  1. RecyclerView 与 ItemTouchHelper 实现拖拽效果

    截图 需求 App 开发新的需求,要求 RecyclerView 实现的九宫格样式可以拖拽,松手以后变更位置,类似于手机桌面拖动 app 变更位置. 分析 经过搜索,发现 support 中带有一个类 ...

  2. SqlServer四种排序:ROW_NUMBER()/RANK()/DENSE_RANK()/ntile() over()

    首先,我们创建一些测试数据. if OBJECT_ID('Tempdb.dbo.#Tmp') is not null drop table #Tmp create table #Tmp ( name ...

  3. 编写一个C语言函数,要求输入一个url,输出该url是首页、目录页或者其他url

    编写一个C语言函数,要求输入一个url,输出该url是首页.目录页或者其他url 首页.目录页或者其他url 如下形式叫做首页: militia.info/ www.apcnc.com.cn/ htt ...

  4. HTML5学习笔记(十六):原型、类和继承【JS核心知识点】

    理解原型 在JavaScript中,只要声明了一个函数,就会为该函数创建一个名为prototype的属性,该属性指向当前函数的原型对象. 而函数的原型对象有一个constructor属性,该属性指向刚 ...

  5. Safe point

    JVM源码分析之安全点safepoint http://www.jianshu.com/p/c79c5e02ebe6 聊聊JVM(六)理解JVM的safepoint 聊聊JVM(九)理解进入safep ...

  6. 在vue-cli搭建的项目中在后台mock接口中支持req.body和req.cookies

    在<vue-cli搭建的项目中增加后台mock接口>中实现了后台mock,但是前端post的t数据都要在mock的后台接口中使用req的接收数据事件获取http协议body中的数据. re ...

  7. linux命令(39):shell 打印偶数行,奇数行 ,行号

    awk 命令: 1. 打印行号和内容: awk '{print NR":"$0}' 2. 输出:偶数行和奇数行到文件 awk '{print $0.txt > NR%2.tx ...

  8. 【工具】Windows7搭建FTP服务器

    有时候需要将文件在各台电脑间拷贝,所以想建一个ftp服务器方便些,这里的设置仅为家用设置的记录日志,严谨的生产环境请参考其他文章. 创建一个专用于ftp的用户 开始 > 控制面板 > 用户 ...

  9. ubuntu下安装vmTools, 和共享文件

    如果没有需要下载一个vmTools,我是下载的 然后加载到光驱,然后在ubuntu下面可以找到光驱 解压里面的文件, 我解压到 Documents下面 然后ctrl + alt + t进入控制台 cd ...

  10. Mac在Finder中显示/usr、/tmp、/var等隐藏目录

    Finder中默认是不显示/usr./tmp./var等隐藏目录的,通过在终端中输入一下命令来另其显示: #开启 defaults write com.apple.Finder AppleShowAl ...