问题描述
我正在做一个 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 上的 ScaleY
和 ScaleX
函数来调整它的大小,但它只会调整所有 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"));
}
希望有人知道怎么做
谢谢
推荐答案
您可以通过编程方式更改 width
和 height
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 日历大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!