简体   繁体   English

CalendarView不显示该月的日期

[英]CalendarView not showing days of the month

I'm doing a calendar app and I encountered a strange problem. 我正在做一个日历应用程序,我遇到了一个奇怪的问题。
If I'm not wrong adding a CalendarView to my layout should let me see the days of the month. 如果我没有错,在我的布局中添加CalendarView应该让我看看这个月的日子。
So I made a new project where the only thing I did was placing the CalendarView on my MainActivity layout. 所以我做了一个新项目,我唯一做的就是将CalendarView放在我的MainActivity布局上。
The problem is that once I run the app I can see only this and there is no error. 问题是,一旦我运行应用程序,我只能看到这个并且没有错误。

package com.example.kanye.kalendarz22;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CalendarView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
CalendarView calendar ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.initializeCalendar();

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}
public void initializeCalendar() {
    calendar = (CalendarView) findViewById(R.id.calendarView);

    calendar.setShowWeekNumber(false);

    calendar.setFirstDayOfWeek(2);

    calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
        @Override
        public void onSelectedDayChange(CalendarView view, int year, int month, int day) {

            Toast.makeText(MainActivity.this, day + "/" + month + "/" + year, Toast.LENGTH_SHORT).show();

        }
    });
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

} }

http://i.imgur.com/iJsEwCc.png?1

http://i.imgur.com/AdciraO.png?1

XML code: XML代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main" tools:context=".MainActivity">

<TextView android:text="Hello World!" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView" />

<CalendarView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/calendarView"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true" />
</RelativeLayout>

Any advice what to do to make it displayed correctly (with the particular days of the month)? 任何建议如何正确显示(与该月的特定日期)? I was looking for permissions to add to manifest, but apparently it doesn't need any. 我一直在寻找添加到清单的权限,但显然它不需要任何权限。 By the way I'm completely aware that I need the min Api level 11 to use this CalendarView. 顺便说一句,我完全知道我需要最小Api等级11来使用这个CalendarView。

Edit: Sorry for not giving you enough information, at the cause: lack of experience and my bad English is limiting me. 编辑:很抱歉没有给你足够的信息,原因是:缺乏经验,我的英语不好限制了我。

After more than a year you probably found a solution or used something else to get rid of the problem. 经过一年多的时间,您可能找到了解决方案或使用其他方法解决问题。 But as I was trying different things to get rid of this strange issue, I managed to find out what's happening. 但是当我尝试不同的东西摆脱这个奇怪的问题时,我设法找出发生了什么。

Starting from API 20, CalendarView was renewed and looks like in your designer screenshot. 从API 20开始,CalendarView得到了更新,看起来就像在设计器截图中一样。 It's still the case for the most recent API 25. 最新的API 25仍然如此。

But for API 19 (and lower I suppose), it looks completely different : 但对于API 19(我认为更低),它看起来完全不同:

适用于Android 4.4的CalendarView(API 19)

And as silly as it seems, the problem comes from the axml layout. 而且看起来很傻,问题来自于axml布局。 This calendar is absolutely unable to handle wrap_content parameter for the calendar's height. 此日历绝对无法处理日历高度的wrap_content参数。 Putting any value in pixels or dp will fix the issue. 以像素或dp放置任何值都可以解决问题。

So, what you can do is add this in your code to change the height of the calendar view for API 19 and lower (I'm using Xamarin so it's C# but it can easily be translated to java) : 所以,你可以做的是在你的代码中添加这个来改变API 19及更低版本的日历视图的高度(我使用的是Xamarin,所以它是C#,但它很容易被翻译成java):

if (((int)Android.OS.Build.VERSION.SdkInt) <= 19) {
    var parameters = myCalendarView.LayoutParameters;
    parameters.Height = 500; //value in pixels
}

It solved the problem for me. 它解决了我的问题。 Sadly this kind of thing is frequent between API 19 and higher APIs.. I would abandon completely support for this API and lower but according to Android stats, there's still 17% of working devices on Android 4.4.. We'll have to deal with it I guess. 遗憾的是,这种事情经常发生在API 19和更高版本的API之间。我会放弃完全支持这个API并降低但是根据Android统计数据,Android 4.4上仍然有17%的工作设备。我们将不得不处理我想。

I have the same issue, as far as I understand it, it has to do with API version. 我有同样的问题,据我所知,它与API版本有关。
The design view is showing you API version 23, while your phone/emulator is lower, <20. 设计视图显示API版本23,而您的手机/模拟器较低,<20。
I'm not sure if this can be fixed at all. 我不确定这是否可以修复。

Try using instead, DatePicker, adding these two lines: 尝试使用DatePicker,添加以下两行:

datePicker.setCalendarViewShown(true);
datePicker.setSpinnerShown(false)

But the fun doesn't stop there, the onDateChanged function is not called in Android 5.0 for datePicker... so have both and remove the irrelevant one is what I did to support every possibility. 但乐趣并没有就此止步,在Android 5.0中没有为datePicker调用onDateChanged函数......所以要同时删除不相关的函数是我为支持所有可能性而做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM