kotlin 材料2日期选择器Android文本大小如此之小

i1icjdpr  于 2023-10-23  发布在  Kotlin
关注(0)|答案(1)|浏览(76)

我在MDC中实现Material 2 DatePicker时遇到了这个问题。文字的大小看起来很小,当运行。我还没有实现任何主题/风格到我的日期选择器
screenshot-datepicker
我看到教程的例子https://www.geeksforgeeks.org/material-design-date-picker-in-android/,它看起来正常,即使没有实现任何风格。
AndroidManifest.xml

<application
    android:name=".MyApplication"
    android:allowBackup="false"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:usesCleartextTraffic="false"
    tools:targetApi="31”>

Fragment.kt

private fun showDatePicker(){
    viewBinding.btnEditProfile.setOnClickListener {
        object : DialogDatePicker(requireActivity()) {
            override fun onSelectedDate(date: String) {
                viewBinding.root.showSnackbar(date)
            }
        }.singleDatePicker(fragmentManager = childFragmentManager,
            startDate = -70,
            endDate = -10)
    }
}

DatePicker

fun singleDatePicker(fragmentManager: FragmentManager, startDate : Int = 0, endDate : Int = 0){

    val today = Date()

    val starCal = Calendar.getInstance()
    starCal.time = today
    starCal.add(Calendar.YEAR, startDate)

    val endCal = Calendar.getInstance()
    endCal.time = today
    endCal.add(Calendar.YEAR, startDate)

    val constraintsBuilder = CalendarConstraints.Builder()
    if (endDate != 0) {
        constraintsBuilder.setEnd(endCal.timeInMillis)
        constraintsBuilder.setOpenAt(endCal.timeInMillis)
    }
    if (startDate != 0)
        constraintsBuilder.setStart(endCal.timeInMillis)

    val builder = MaterialDatePicker.Builder.datePicker()
        .setTitleText("Pilih Tanggal")
        .setCalendarConstraints(constraintsBuilder.build())

    val picker = builder.build()

    // Set a listener to handle the selected date
    picker.addOnPositiveButtonClickListener { selection ->
        if (selection != null) {
            // Handle the selected date (selectedDate)
            val calendar = Calendar.getInstance()
            calendar.timeInMillis = selection
            val year = calendar.get(Calendar.YEAR)
            val month = calendar.get(Calendar.MONTH)
            val day = calendar.get(Calendar.DAY_OF_MONTH)
            // Do something with the selected date
            onSelectedDate(
                year.toString()+"-"+ String.format(
                    "%02d",
                    month + 1
                )+ "-" + String.format("%02d", day)
            )
        }
    }

    // Show the date picker
    picker.show(fragmentManager, picker.toString())
}

Theme.xml

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name=“AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/blue_500</item>
    <item name="colorPrimaryDark">@color/blue_800</item>
    <item name="colorAccent">@color/blue_300</item>
    <item name="colorPrimaryVariant">@color/blue_650</item>
    <item name="colorOnPrimary">@color/white</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/blue_800</item>
    <item name="colorSecondaryVariant">@color/blue_800</item>
    <item name="colorOnSecondary">@color/blue_200</item>

    <item name="android:windowBackground">@color/blue_700</item>
    <item name="android:colorForeground">@color/blue_600</item>
    <item name="buttonTint">?attr/colorPrimary</item>
    <item name="textAppearanceCaption">@color/caption</item>
    <item name="textAppearanceBodySmall">@color/blue_100</item>
    <item name="textAppearanceBodyMedium">@color/blue_100</item>
    <item name="textAppearanceBodyLarge">@color/blue_100</item>
    <item name="textAppearanceSubtitle1">@color/blue_100</item>
    <item name="textAppearanceSubtitle2">@color/blue_100</item>
    <item name="textAppearanceHeadline5">@color/blue_100</item>
    <item name="textAppearanceHeadline4">@color/blue_100</item>
    <item name="textAppearanceHeadline3">@color/blue_100</item>
    <item name="textAppearanceHeadline2">@color/blue_100</item>
    <item name="textAppearanceHeadline1">@color/blue_100</item>
    <item name="colorOutline">@color/blue_500</item>
    <item name="hintTextColor">@color/grey_cold</item>
    <item name="indicatorColor">@color/light_grey</item>
    <item name="colorTertiary">@color/blue_200</item>
    <item name="colorOnTertiary">@color/blue_700</item>
    <item name="android:activatedBackgroundIndicator">@color/grey</item>

    <!-- Status bar color. -->
    <item name="android:statusBarColor">@color/blue_700</item>
    <!-- Customize your theme here. -->

</style>
bihw5rsg

bihw5rsg1#

好吧,当我把你的风格直接复制到我的测试应用程序中时,我终于能够重现这个问题。问题是所有的 *textAppearance... * 属性-当我删除它们时,一切都恢复正常。
我不太熟悉Material DatePickers,但从名称猜测,我认为任何 *textAppearance. * 属性都应该分配一个TextAppearance值,而不是颜色。橙色可能会导致一些意想不到的行为,这是你刚刚经历的。

相关问题