【问题标题】:Hide "selected date" part of DatePicker隐藏 DatePicker 的“选定日期”部分
【发布时间】:2017-08-10 16:31:32
【问题描述】:

我创建了一个自定义对话框,其中我在视图的顶部有一个 DatePicker,在下部有一个带有 AM/PM 单选按钮的 NumberPicker。工作正常,但默认情况下 DatePicker 在顶部有这个额外的区域,显示“选定的日期”,如下面的屏幕截图所示。如何删除它,以便我在日期选择器区域中拥有的只是日历本身?这可能吗?我要删除的部分是黄色的。

datetime_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/layoutDateTimePickerContainer"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">

    <DatePicker
        android:id="@+id/dialogDatePicker"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center_vertical|center_horizontal"
        android:orientation="horizontal">

        <NumberPicker
            android:id="@+id/dialogTimePicker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />    

        <RadioGroup
            android:id="@+id/radgrpDateTimePicker"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginStart="20dp"
            android:checkedButton="@+id/radDateTimePickerAM"
            android:gravity="center_vertical">

            <RadioButton
                android:id="@+id/radDateTimePickerAM"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="AM"/>

            <RadioButton
                android:id="@+id/radDateTimePickerPM"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="PM"/>

        </RadioGroup>

    </LinearLayout>

    <TableRow
        android:id="@+id/trDateTimePickerButtonsContainer"
        style="?android:attr/buttonBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="end"
        android:padding="10dp">

        <Button
            android:id="@+id/btnDateTimePickerCancel"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="20dp"
            android:text="@string/general_cancel"
            android:textAllCaps="false"/>

        <Button
            android:id="@+id/btnDateTimePickerOkay"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/general_okay"
            android:textAllCaps="false"/>

    </TableRow>

</LinearLayout>

【问题讨论】:

  • 请提供您的自定义对话框布局
  • @H.Brooks - 根据要求提供 XML。谢谢!

标签: android android-layout android-datepicker


【解决方案1】:

请试试这个:

dialogDatePicker.setTitle("");

你试过这样吗:

DatePickerDialog dialog= new DatePickerDialog(this, dateSetListener, year, month, day);
dialog.setTitle("");

【讨论】:

  • 没有区别。 - 对话框标题不是问题,而是对话框中的 datepicker 对象本身
  • 我很欣赏您的想法,但是这是使用我在帖子中指定的自定义对话框。不是 Android 中的 DatePickerDialog 类。在对话框本身上调用 .setTitle 对对话框布局中的 DatePicker 对象没有影响
【解决方案2】:

想通了。您必须参考 DatePicker 本身的 LinearLayout,然后 .getChildAt(0) 并隐藏它。

Dialog dialog = new Dialog(context);
dialog.setContentView(R.id.datetime_dialog);
DatePicker datePicker = dialog.findViewById(R.id.dialogDatePicker);
LinearLayout datePickerHeader = (LinearLayout) datePicker.getChildAt(0);
datePickerHeader.setVisibility(View.GONE);

【讨论】:

    猜你喜欢
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-10
    • 2021-12-21
    • 1970-01-01
    • 2014-11-04
    相关资源
    最近更新 更多