【问题标题】:Fullscreen DatePicker inside ActivityActivity 中的全屏 DatePicker
【发布时间】:2017-09-12 11:36:09
【问题描述】:

我试图在我使用 startActivityForResult 调用的活动中显示 datePicker 和 timePicker 小部件。两个选择器都应该匹配父布局宽度。在我的情况下,它可以与 timePicker 一起使用,但 datePicker 不会填满整个屏幕。

我尝试以编程方式测量显示大小并在 onResume 中设置 datePicker 的布局参数。但不幸的是,它没有奏效。

如何实现 datePicker 小部件,使其延伸到父布局的宽度?

datePicker 截图

timePicker 截图

activity_date_time_picker.xml:

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

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ScrollView
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar">

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

            <DatePicker
                android:id="@+id/datePicker"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center" />

            <TimePicker
                android:id="@+id/timePicker"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center" />

        </LinearLayout>
    </ScrollView>
</RelativeLayout>

DateTimePickerActivity 中的onResume 方法:

/* ... */
@Override
protected void onResume() {
    super.onResume();

    int width = Resources.getSystem().getDisplayMetrics().widthPixels;
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) datePicker.getLayoutParams();
    params.width = width;
    datePicker.setLayoutParams(params);
}
/* ... */

【问题讨论】:

  • 可能你应该是这样的:imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

标签: java android xml datepicker android-datepicker


【解决方案1】:

试试这个

Style.xml

<style name="DialogTheme" parent="Theme.AppCompat.Light">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

MainActivity.java 将主题应用到日期选择器 (R.style.AppTheme) 的构造函数中


 DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), R.style.AppTheme, this, year, month, day);
            datePickerDialog.getDatePicker().setMaxDate(c.getTimeInMillis());
            return datePickerDialog;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-28
    • 1970-01-01
    • 2017-04-11
    • 2011-05-19
    • 1970-01-01
    相关资源
    最近更新 更多