• 安卓——CalendarView 无法显示日期
  • 雪峰 发表于 2016-08-02 标签:
  • 对,最近又开始学习安卓了,毕竟有想做的app。今天在使用CalendarView 这个组件时,遇到了一个麻烦,就是只显示年份及月份,但是日期完全不显示!!!我自以为是按照书上的代码来的肯定没错,总觉得肯定又是bug。呵呵,结果居然就是因为改了一行代码又浪费了一个多小时。

    没改之前是这样的代码:

    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    
    <CalendarView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:firstDayOfWeek="2"
    android:id="@+id/calendar"
    />
    
    </LinearLayout>
    
    

    效果就是这个样子:

    device-2016-08-02-163124

    然后,去google了一下,终于发现问题出在哪了,问题来源于stackoverflow.com(这可是个好网站,遇到bug及时去找,很容易找到同样的问题,不像某度):Android CalendarView not displaying the days,仅仅是因为把calendarView的layout_height改为wrap_content才出现了这种结果!!!需要设置为match_parent就不会出现上述情况了。(书上就是match_parent,所以书上代码是正确的,这个锅该自己背

    
    <CalendarView
    android:layout_width="match_parent"
    android:layout_height="match_parent"  没错,就是这的问题 ,wrap_content不会显示  
    android:firstDayOfWeek="2"
    android:id="@+id/calendar"
    />
    
    

    修改后效果图:

    device-2016-08-02-163842

    这样一来就能正常显示了。

    教训:作为新手,不要随意改动书上例子的代码,否则很容易遇到“麻烦”,除非很有把握那么做的结果。还有,不要总是一心在百度上寻找答案,还是多学点英语,用google找问题比较快一点,免得走弯路。

    发表回复