本文介绍了以编程方式在 Android 中更改 DatePicker 日历大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个 Android 应用程序,我必须显示一个 DatePickerDialog.事实是,该应用程序将在具有智能屏幕的设备上运行,而日历不适合其中.我希望日历更小,同时保持相同的比例.

I'm doing an Android application and I have to show a DatePickerDialog. The fact is that the application will be on a device with a smart screen and the calendar doesn't fit in it. I would like the calendar to be smaller while keeping the same proportions.

看起来像这样

我尝试使用 DatePicker 上的 ScaleYScaleX 函数来调整它的大小,但它只会调整所有 DatePicker 的大小.

I tried to use the ScaleY and ScaleX functions on the DatePicker to resize it but it just resizes all the DatePickers.

喜欢这个

这是我用来显示 DatePickerDialog 的代码

Here's the code I use to show the DatePickerDialog

public void ShowDate() {
    final Calendar c = Calendar.getInstance();


    year = c.get(Calendar.YEAR);
    month = c.get(Calendar.MONTH);
    day = c.get(Calendar.DAY_OF_MONTH);

    DatePickerDialog dateForm = new DatePickerDialog(ViewForm.this, R.style.ColorOne, new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {

        }
    }, year, month, day);
    dateForm.show();

    DatePicker dp = dateForm.getDatePicker();

    dp.setScaleY(Float.parseFloat("0.5"));
    dp.setScaleX(Float.parseFloat("0.5"));
}

希望有人知道怎么做

谢谢

推荐答案

您可以通过编程方式更改 widthheight DatePickerDialog,您只需使用 WindowManager.LayoutParams params= dateForm.getWindow().getAttributes(); 看这个例子:

you can change width and height DatePickerDialog programmatically, you just use WindowManager.LayoutParams params = dateForm.getWindow().getAttributes(); see this example:

        final Calendar c = Calendar.getInstance();

        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog dateForm = new DatePickerDialog(this, R.style.AppTheme, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {

            }
        }, year, month, day);

        DatePicker dp = dateForm.getDatePicker();

        WindowManager.LayoutParams params = dateForm.getWindow().getAttributes();

        params.gravity = Gravity.TOP;
//        params.x = 0; // locationByX;
//        params.y = 0; // locationByY;
        params.width = 600; // dialogWidth;
//        params.height = 1000; // dialogHeight;

        dateForm.getWindow().setAttributes(params);

        dateForm.show();

//        dp.setScaleY(Float.parseFloat("0.7"));
//        dp.setScaleX(Float.parseFloat("0.7"));

这篇关于以编程方式在 Android 中更改 DatePicker 日历大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 07:17