You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android日期选择器中如何用Drawable资源修改指定日期背景

用Drawable资源实现普通与重要日期的区分显示

日期区分显示效果

比如图中的17号和26号,通过不同背景区分普通日期和重要日期,下面直接给出Drawable的实现方案:

1. 创建两种背景Drawable文件

普通日期背景(res/drawable/normal_date_bg.xml)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <!-- 普通日期填充色,用浅色或默认背景色 -->
    <solid android:color="#FFFFFF" />
    <!-- 细边框区分 -->
    <stroke
        android:width="1dp"
        android:color="#E0E0E0" />
    <!-- 统一大小,保证所有日期控件一致 -->
    <size
        android:width="40dp"
        android:height="40dp" />
</shape>

重要日期背景(res/drawable/important_date_bg.xml)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <!-- 用醒目的填充色突出重要日期 -->
    <solid android:color="#FF4081" />
    <!-- 边框同填充色,强化视觉效果 -->
    <stroke
        android:width="1dp"
        android:color="#FF4081" />
    <!-- 和普通日期保持相同大小 -->
    <size
        android:width="40dp"
        android:height="40dp" />
</shape>

2. 在布局或代码中使用

静态布局中直接设置

<!-- 普通日期TextView -->
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/normal_date_bg"
    android:gravity="center"
    android:text="15"
    android:textColor="#333333" />

<!-- 重要日期TextView -->
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/important_date_bg"
    android:gravity="center"
    android:text="17"
    android:textColor="#FFFFFF" />

代码中动态切换(适合日历列表场景)

// 根据日期类型判断是否为重要日期
if (isImportantDate) {
    dateTv.setBackgroundResource(R.drawable.important_date_bg);
    dateTv.setTextColor(getResources().getColor(R.color.white));
} else {
    dateTv.setBackgroundResource(R.drawable.normal_date_bg);
    dateTv.setTextColor(getResources().getColor(R.color.black));
}

内容的提问来源于stack exchange,提问作者Siva Siva

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 18:17:19