Android res

一、ColorStateList

       Android中我们可以为图片设置状态切换,创建一个selector即可,设置按下、选中、聚焦等状态下的图片,即可让空间在不同状态下自动切换图片。对于字体颜色如果也想要同样的效果,又该怎么做呢?

       很简单,Android为开发者提供了ColorStateList接口,创建ColorStateList和创建drawable同样,见附件图片。

 <?xml version="1.0" encoding="utf-8"?>

    <selector xmlns:android="http://schemas.android.com/apk/res/android" >

        <item

            android:color="hex_color"

            android:state_pressed=["true" | "false"]

            android:state_focused=["true" | "false"]

            android:state_selected=["true" | "false"]

            android:state_active=["true" | "false"]

            android:state_checkable=["true" | "false"]

            android:state_checked=["true" | "false"]

            android:state_enabled=["true" | "false"]

            android:state_window_focused=["true" | "false"] />

    </selector>

使用时需要注意:

1、如果在xml中使用,直接将textColor属性指向这个文件即可,即@color/name

2、如果在Java代码中使用,setTextColor没有效果,需要使用setTextColor(getResources().getColorStateList(R.color.color_sel))

 

二、string

Context有

public final String getString (int resId)
Added in  API level 1

Return a localized string from the application's package's default string table.

Parameters
resIdResource id for the string

public final String getString (int resId, Object... formatArgs)
Added in  API level 1

Return a localized formatted string from the application's package's default string table, substituting the format arguments as defined in Formatter and format(String, Object...).

Parameters
resIdResource id for the format string
formatArgsThe format arguments that will be used for substitution.

第二个方法的使用要求在value文件夹下的string中预定义待插入的内容的位置

   <string name="record">%1$s(%2$d条记录)</string>

使用的时候,getString(R.string.record, "test", 23)即可生成test(23条记录的字串)

 

%1$s表示第一个待填项在这个位置,s表示是个字符串

%2$d表示第二个待填项在这个位置,d表示是个数字

数字必须对应顺序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值