Android:如何在 YEAR/MONTH/DATE Spinners 中获取日历

Android: How to get the Calendar in YEAR/MONTH/DATE Spinners

在我的 Android 应用中,我试图获取用户的生日。下面是我的 XML 文档 .

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@color/colorPurple"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            style="@style/MyDrawerArrowToggle"
            android:layout_marginTop="@dimen/toolbar_layout_top_margin">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:orientation="horizontal">

                <LinearLayout
                    android:layout_width="30dp"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:gravity="center">

                    <ImageView
                        android:id="@+id/signUp_back_button"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/back_arrow_white" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="220dp"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:textSize="20dp"
                        android:text="@string/birthday"
                        android:textColor="@color/colorWhite"
                        android:gravity="center|left"
                        android:paddingLeft="20dp"/>

                </LinearLayout>

            </LinearLayout>

        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="18dp"
                android:textStyle="bold"
                android:text="@string/what_is_your_birthday"
                android:gravity="center"
                android:layout_marginTop="60dp"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginLeft="30dp"
                android:layout_marginRight="30dp"
                android:layout_marginTop="30dp">

                <DatePicker
                    android:layout_width="match_parent"
                    android:layout_height="150dp"
                    android:datePickerMode="spinner"
                    android:id="@+id/date_picker"/>



                <!--<com.aigestudio.wheelpicker.widgets.WheelDayPicker-->
                <!--android:layout_width="50dp"-->
                <!--android:layout_height="match_parent" />-->

                <!--<com.aigestudio.wheelpicker.widgets.WheelMonthPicker-->
                <!--android:layout_width="50dp"-->
                <!--android:layout_height="match_parent"-->
                <!--android:layout_marginLeft="20dp"/>-->

                <!--<com.aigestudio.wheelpicker.widgets.WheelYearPicker-->
                <!--android:layout_width="50dp"-->
                <!--android:layout_height="match_parent"-->
                <!--android:layout_marginLeft="20dp"/>-->

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:background="@color/colorPurple"
                android:layout_marginTop="40dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:id="@+id/signup_birthday_next_button_layout">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text = "@string/next_text"
                    android:textColor="@color/colorWhite"
                    android:textSize="14dp"/>

            </LinearLayout>

        </LinearLayout>

    </ScrollView>

</LinearLayout>

这给了我下面的 UI

但我正在寻找下面的

如何获得第二个UI?

您可以使用 SpinnerDatePicker 开源库,它有 UI 如下所示 由于它是开源的,您可以根据需要进行更改。

请添加此 xml 文件,它可能有助于解决您的问题。

<LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <TextView
        android:gravity="center"
        android:id="@+id/btnNotification"
        android:text="What is your birthday"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <DatePicker
        android:id="@+id/spinnerDatePicker"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:calendarViewShown="false"
        android:datePickerMode="spinner"
        android:descendantFocusability="blocksDescendants"
        android:focusable="true"
        android:spinnersShown="true" />

    <Button
        android:text="next"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>