gpt4 book ai didi

android - 更改 DatePicker 背景颜色

转载 作者:太空狗 更新时间:2023-10-29 16:11:09 25 4
gpt4 key购买 nike

我正在尝试在另一个 Activity 之上显示一个 DatePicker 对话框,发生的事情是它以某种方式继承了它的颜色。

我希望它有一个绿色的标题和白色的背景, like on this example

这里是风格的摘录

<style name="DatePickerDialog" parent="@android:style/Theme.Holo.Light">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="colorAccent">@color/primary</item>
</style>

而这段代码是用来弹出DatePicker

    DatePickerDialog datepicker = new DatePickerDialog(this, R.style.DatePickerDialog, new DatePickerDialog.OnDateSetListener() {

public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
TextView newdate = (TextView) findViewById(R.id.newdate);
Date date = getDate(year, monthOfYear, dayOfMonth);
DateFormat dateformat = new SimpleDateFormat(getResources().getString(R.string.date_format_full));
newdate.setText(dateformat.format(date));
}
}, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

datepicker.show();

However it instead is displayed in all green

如果我在样式中指定白色背景,it overrides the header and the buttons as well

    <item name="android:background">@color/app_background</item>

我尝试的最后一件事是使用 AlertDialog.THEME_DEVICE_DEFAULT_DARK 作为 DatePicker 主题

DatePickerDialog datepicker = new DatePickerDialog(this, 
AlertDialog.THEME_DEVICE_DEFAULT_DARK, new
DatePickerDialog.OnDateSetListener()

But it'd still display the whole dialog in green

这是我从中打开对话框的 Activity 的样式

<style name="UserDialog" parent="android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:background">@color/primary</item>
<item name="android:textColor">@color/dialog_text</item>
<item name="colorPrimary">@color/app_background</item>
<item name="android:windowTitleStyle">@style/NewDialogTitle</item>
</style>

<style name="NewDialogTitle" parent="@android:style/TextAppearance.DialogWindowTitle">
<item name="android:gravity">center_horizontal</item>
</style>

以及我使用的颜色

<color name="primary">#4CAF50</color>
<color name="app_background">#FFFFFF</color>

有人知道怎么做吗?我将不胜感激任何指导。我试着关注 this answer, but had no luck

最佳答案

这段代码对我有用试试这个...

样式.xml

<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">@android:color/holo_green_dark</item>
</style>

弹出代码

        Calendar mcurrentDate = Calendar.getInstance();
int mYear = mcurrentDate.get(Calendar.YEAR);
int mMonth = mcurrentDate.get(Calendar.MONTH);
int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);

DatePickerDialog mDatePicker;
mDatePicker = new DatePickerDialog(context, R.style.DialogTheme, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {

Toast.makeText(context,"Selected Date " + + selectedday + "-" + ++selectedmonth + "-" + selectedyear ,Toast.LENGTH_SHORT).show();
}
}, mYear, mMonth, mDay);
mDatePicker.show();

enter image description here

关于android - 更改 DatePicker 背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48359176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com