Android Resources ---color about text

本文详细介绍了如何在Java中使用XML配置静态颜色资源,包括颜色选择器、颜色状态和应用颜色到视图的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Note:If you want to provide a static color resource,use a simple color value.

In Java: R.color.filename
In XML: @[package:]color/filenameSYNTAX:

<?xml version="1.0" encoding="utf-8"?>
<selectorxmlns: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_checkable=["true" | "false"]
       
android:state_checked=["true" | "false"]
       
android:state_enabled=["true" | "false"]
       
android:state_window_focused=["true" | "false"] />
</selector>

1>selector:颜色源文件的根元素
2>item:定义某一特定状态下的颜色属性包括”state“和color

state:
android:state_pressed 是否按下
android:state_focused 是否取得焦点
android:state_selected 是否被选择
android:state_checkabled 组件是否能被check
android:state_checked 被check了//
android:state_enabled 能够接受触摸或者接触事件
android:state_window_focused 应用程序是否在前台

注:对于不同状态的item,程序会自上而下的匹配,最先匹配的得到程序

XML file saved at res/color/button_text.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   
<item android:state_pressed="true"
         
android:color="#ffff0000"/><!-- pressed -->
   
<item android:state_focused="true"
         
android:color="#ff0000ff"/><!-- focused -->
   
<item android:color="#ff000000"/><!-- default -->
</selector>
 
 

This layout XML will apply the color list to a View:

<Button
   
android:layout_width="fill_parent"
   
android:layout_height="wrap_content"
   
android:text="@string/button_text"
   
android:textColor="@color/button_text"/>

注:同样的方法可以设置View的背景,以drawable Resource代替color Resource也可行。




转载于:https://www.cnblogs.com/LVAnny/p/I_WANT_PROVIDE_A_STATIC_COLOR_RESOURCE.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值