首页 > 解决方案 > 如何禁用 CalendarView 的滚动 - Android Dev

问题描述

我目前正在刷新我的 android 开发技能并遇到了CalendarView组件。我想禁用日历的滚动,而是启用箭头浏览日历。

我想知道以下内容: 如何禁用 CalendarView 的滚动?为什么 Android Studio 在设计器模式中向我显示箭头浏览版本,但编译并运行日历的滚动版本?

我正在运行一个 Android VM,一个带有 API 22 的“Galaxy Nexus”。我还在下面发布了“build.gradle”。

Android 向我展示了什么/我想要什么:

在此处输入图像描述

Android Studio 编译和运行的内容:

在此处输入图像描述


这里有一些代码:

// activity_main.java:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        tools:layout_editor_absoluteX="0dp">

        <CalendarView
            android:id="@+id/calendarView4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:isScrollContainer="false" />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

// build.gradle

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig {
        applicationId "com.example.kalender003_emptyactivity"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

标签: javaandroidcalendarview

解决方案


使用 MaterialComponents 主题,它很可能会解决问题。

在您的值/主题文件中,用这个替换它们的父级,

parent="Theme.MaterialComponents.DayNight.DarkActionBar"

如果您不想要操作栏,那么,

parent="Theme.MaterialComponents.DayNight.NoActionBar"

如果您只想显示浅色日历视图而不考虑设备的主题使用,

parent="Theme.MaterialComponents.Light.NoActionBar"

推荐阅读