CalendarView未显示每月的某天 [英] CalendarView not showing days of the month

查看:58
本文介绍了CalendarView未显示每月的某天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个日历应用程序,但遇到一个奇怪的问题.
如果我没看错,则将CalendarView添加到我的布局中应该让我看到一个月中的日子.
因此,我做了一个新项目,唯一要做的就是将CalendarView放在MainActivity布局上.
问题是,一旦我运行该应用程序,我只能看到此内容,并且没有错误.

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.
So I made a new project where the only thing I did was placing the CalendarView on my MainActivity layout.
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);
}

}

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>

任何建议如何使它正确显示(在每月的特定日期)?我一直在寻找添加到清单的权限,但显然不需要任何权限.顺便说一句,我完全意识到我需要最低Api级别11才能使用此CalendarView.

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.

很抱歉没有给您足够的信息,原因是:经验不足,英语不好限制了我.

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.

从API 20开始,CalendarView已更新,外观类似于您的设计器屏幕快照.最新的API 25仍然如此.

Starting from API 20, CalendarView was renewed and looks like in your designer screenshot. It's still the case for the most recent API 25.

但是对于API 19(我想应该是更低的),它看起来完全不同:

But for API 19 (and lower I suppose), it looks completely different :

看起来很愚蠢,问题出在axml布局.该日历绝对无法处理日历高度的wrap_content参数.将任何值设置为像素或dp将解决此问题.

And as silly as it seems, the problem comes from the axml layout. This calendar is absolutely unable to handle wrap_content parameter for the calendar's height. Putting any value in pixels or dp will fix the issue.

因此,您可以做的是在代码中添加此代码,以更改API 19和更低版本的日历视图的高度(我使用Xamarin,所以它是C#,但可以轻松地转换为Java):

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) :

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

它为我解决了问题.可悲的是,这种情况在API 19和更高版本的API之间经常发生..我会完全放弃对该API或更低版本的支持,但根据Android统计,Android 4.4上仍有17%的工作设备.我猜.

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.

这篇关于CalendarView未显示每月的某天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆