DatePicker天被切断

时间:2017-10-29 22:37:52

标签: android datepicker

我正在尝试使用其中包含DatePicker的AlertDialog模式。但是,当我在编辑器中将DatePicker添加到模态时,底部日期被截断: enter image description here

当然,在模拟器上运行时,最后一行日期会被切断:

enter image description here

我已经尝试过填充和边距甚至高度更高但没有任何效果。

编辑:下面是我使用DatePicker的模态的XML代码。

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <DatePicker
        android:id="@+id/dose_date_picker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:datePickerMode="spinner"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="8dp" />
</android.support.constraint.ConstraintLayout>

2 个答案:

答案 0 :(得分:0)

只需使用CalendarView而不是DatePicker。它没有那个错误。

答案 1 :(得分:0)

找到了一个可行的解决方案。 对问题的一点描述here。 DatePicker 使用 this layout 进行日历视图:

    <CalendarView
        android:id="@+id/calendar_view"
        android:layout_width="245dip"
        android:layout_height="280dip"

基本上是固定的宽度和高度。这曾经适用于 holo 主题,即 android:Theme.Holo.Light,因为它使用 legacy xml layout,但是对于 appcompat 主题或材料主题,它开始切断日历中的最后一行,因为他们使用的是 different layout

解决方案是以编程方式更新日历视图高度并使其包裹内容。

CalendarView cv = datePicker.getCalendarView();
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) cv.getLayoutParams();
params.height = LinearLayout.LayoutParams.WRAP_CONTENT; // Or 300dp
cv.setLayoutParams(params);