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
resId
Resource 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
resId formatArgs
Resource id for the format string
The 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表示是个数字

数字必须对应顺序

猜你喜欢

转载自zanelee007.iteye.com/blog/1773596
今日推荐