카테고리 보관물: Android

Android

Android 5.0에서 DatePicker 대화 상자 색상을 변경하는 방법 (및 timepicker) 색 구성표를 변경할

Android 5.0의 datepicker (및 timepicker) 색 구성표를 변경할 수 있습니까?

강조 색상을 설정하려고 시도했지만 작동하지 않습니다 (포함 및 제외 android:).

<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">@color/purple</item>

<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">@color/purple_tint</item>

<!-- colorAccent is used as the default value for colorControlActivated
     which is used to tint widgets -->
<item name="colorAccent">@color/purple_tint</item>

원본에서 :
여기에 이미지 설명 입력

다음과 같이 :



답변

Neil의 제안이 전체 화면으로 표시되는 이유 DatePicker는 상위 테마를 선택하기 때문입니다.

<!-- Theme.AppCompat.Light is not a dialog theme -->
<style name="DialogTheme" parent="**Theme.AppCompat.Light**">
    <item name="colorAccent">@color/blue_500</item>
</style>

또한이 경로로 이동하면 다음을 생성하는 동안 테마를 지정해야합니다 DatePickerDialog.

// R.style.DialogTheme
new DatePickerDialog(MainActivity.this, R.style.DialogTheme, new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        //DO SOMETHING
    }
}, 2015, 02, 26).show();

제 생각에는 이것은 좋지 않습니다. 자바와 styles.xml / themes.xml 내부의 스타일을 유지해야합니다.

나는 Neil의 제안이 약간의 변화 (상위 테마를 변경 Theme.Material.Light.Dialog)로 원하는 결과를 얻을 것이라는 데 동의합니다 . 하지만 다른 방법이 있습니다.

첫 번째 검사에서 (변경하려는 내용) , 및 기타 몇 가지 텍스트 색상 및 텍스트 스타일과 datePickerStyle같은 사항을 정의 합니다.headerBackgrounddayOfWeekBackground

앱 테마에서이 속성을 재정의하면 작동하지 않습니다. DatePickerDialog속성에서 할당 할 수있는 별도의 테마를 사용합니다 datePickerDialogTheme. 따라서 변경 사항을 적용하려면 overriden datePickerStyle내부에서 재정의datePickerDialogTheme 해야합니다 .

여기 있습니다 :

datePickerDialogTheme앱의 기본 테마 내에서 재정 의 :

<style name="AppBaseTheme" parent="android:Theme.Material.Light">
    ....
    <item name="android:datePickerDialogTheme">@style/MyDatePickerDialogTheme</item>
</style>

정의 MyDatePickerDialogTheme. 상위 테마의 선택은 앱의 기본 테마가 무엇인지에 따라 달라집니다. Theme.Material.Dialog또는 Theme.Material.Light.Dialog다음 중 하나 일 수 있습니다 .

<style name="MyDatePickerDialogTheme" parent="android:Theme.Material.Light.Dialog">
    <item name="android:datePickerStyle">@style/MyDatePickerStyle</item>
</style>

우리는 datePickerStyle스타일로 재정의 했습니다 MyDatePickerStyle. 부모의 선택은 앱의 기본 테마가 Widget.Material.DatePicker또는 Widget.Material.Light.DatePicker. 요구 사항에 따라 정의하십시오.

<style name="MyDatePickerStyle" parent="@android:style/Widget.Material.Light.DatePicker">
    <item name="android:headerBackground">@color/chosen_header_bg_color</item>
</style>

현재, 우리는 headerBackground기본적으로 설정되어있는 것만 재정의 하고 ?attr/colorAccent있습니다 (이것이 Neil 제안이 배경을 변경하는 데 작동하는 이유이기도합니다). 그러나 가능한 많은 사용자 정의가 있습니다.

dayOfWeekBackground
dayOfWeekTextAppearance
headerMonthTextAppearance
headerDayOfMonthTextAppearance
headerYearTextAppearance
headerSelectedTextColor
yearListItemTextAppearance
yearListSelectorColor
calendarTextColor
calendarSelectedTextColor

이 정도의 제어 (사용자 지정)를 원하지 않는 경우을 재정의 할 필요가 없습니다 datePickerStyle. colorAccent대부분의 DatePicker's색상을 제어 합니다. 따라서 colorAccent내부 재정의 MyDatePickerDialogTheme가 작동합니다.

<style name="MyDatePickerDialogTheme" parent="android:Theme.Material.Light.Dialog">
    <item name="android:colorAccent">@color/date_picker_accent</item>

    <!-- No need to override 'datePickerStyle' -->
    <!-- <item name="android:datePickerStyle">@style/MyDatePickerStyle</item> -->
</style>

재정의 colorAccent는 변경 OKCANCEL텍스트 색상 의 추가 이점을 제공합니다 . 나쁘지 않다.

이렇게하면 DatePickerDialog's생성자에 스타일링 정보를 제공 할 필요가 없습니다 . 모든 것이 올바르게 연결되었습니다.

DatePickerDialog dpd = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

    }
 }, 2015, 5, 22);

 dpd.show();

답변

이것을 시도하십시오.

코드

new DatePickerDialog(MainActivity.this, R.style.DialogTheme, new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        //DO SOMETHING
    }
}, 2015, 02, 26).show();

styles.xml 파일 의 스타일

편집-테마가 제안 된대로 Theme.AppCompat.Light.Dialog로 변경되었습니다.

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

답변

새로운 스타일 만들기

<!-- Theme.AppCompat.Light.Dialog -->
<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">@color/blue_500</item>
</style>

자바 코드 :

여기에서 부모 테마가 핵심입니다. 색상을 선택하세요

DatePickerDialog = new DatePickerDialog(context,R.style.DialogTheme,this,now.get(Calendar.YEAR),now.get(Calendar.MONTH),now.get(Calendar.DAY_OF_MONTH);

결과:


답변

시도 해봐, 나를 위해 일해

두 가지 옵션을 입력 colorAccent하고android:colorAccent

<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
   ....
    <item name="android:dialogTheme">@style/AppTheme.DialogTheme</item>
    <item name="android:datePickerDialogTheme">@style/Dialog.Theme</item>
</style>

<style name="AppTheme.DialogTheme" parent="Theme.AppCompat.Light.Dialog">

<!-- Put the two options, colorAccent and android:colorAccent. -->
    <item name="colorAccent">@color/colorPrimary</item>
    <item name="android:colorPrimary">@color/colorPrimary</item>
    <item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="android:colorAccent">@color/colorPrimary</item>
 </style>

답변

언급하자면, android.R.style.Theme_DeviceDefault_Light_Dialog대신 기본 테마를 사용할 수도 있습니다 .

new DatePickerDialog(MainActivity.this, android.R.style.Theme_DeviceDefault_Light_Dialog, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    //DO SOMETHING
    }
}, 2015, 02, 26).show();

답변

테마를 만들지 않고 대화 만들기 개체에 작성하십시오.

DatePickerDialog datePicker = new DatePickerDialog(getActivity(), AlertDialog.THEME_HOLO_LIGHT,this, mYear, mMonth, mDay);

이걸 따라하면 모든 유형의 날짜 선택기 스타일이 제공됩니다.

http://www.android-examples.com/change-datepickerdialog-theme-in-android-using-dialogfragment/


답변

Android의 API 24 화면에 시간 선택기 버튼이 표시되지 않는 문제가있었습니다. (API 21+) 다음으로 해결됩니다.

<style name="MyDatePickerDialogTheme" parent="android:Theme.Material.Light.Dialog">
            <item name="android:colorAccent">@color/colorPrimary2</item></style>

<style name="Theme" parent="BBaseTheme">
         <item name="android:datePickerDialogTheme">@style/MyDatePickerDialogTheme</item>
</style>