前言
最近使用日曆,在沒有設置樣式的情況下,如下圖效果是這樣的,?完全白花花一片完全找到到按鈕在哪裏啊,然後就是各種調試~此處略
一 AlertDialog 樣式
我是知道AlertDialog 有如下樣式
/**
* Special theme constant for {@link #AlertDialog(Context, int)}: use
* the traditional (pre-Holo) alert dialog theme.
*
* @deprecated Use {@link android.R.style#Theme_Material_Dialog_Alert}.
*/
@Deprecated
public static final int THEME_TRADITIONAL = 1;
/**
* Special theme constant for {@link #AlertDialog(Context, int)}: use
* the holographic alert theme with a dark background.
*
* @deprecated Use {@link android.R.style#Theme_Material_Dialog_Alert}.
*/
@Deprecated
public static final int THEME_HOLO_DARK = 2;
/**
* Special theme constant for {@link #AlertDialog(Context, int)}: use
* the holographic alert theme with a light background.
*
* @deprecated Use {@link android.R.style#Theme_Material_Light_Dialog_Alert}.
*/
@Deprecated
public static final int THEME_HOLO_LIGHT = 3;
/**
* Special theme constant for {@link #AlertDialog(Context, int)}: use
* the device's default alert theme with a dark background.
*
* @deprecated Use {@link android.R.style#Theme_DeviceDefault_Dialog_Alert}.
*/
@Deprecated
public static final int THEME_DEVICE_DEFAULT_DARK = 4;
/**
* Special theme constant for {@link #AlertDialog(Context, int)}: use
* the device's default alert theme with a light background.
*
* @deprecated Use {@link android.R.style#Theme_DeviceDefault_Light_Dialog_Alert}.
*/
@Deprecated
public static final int THEME_DEVICE_DEFAULT_LIGHT = 5;
/**
* No layout hint.
* @hide
*/
public static final int LAYOUT_HINT_NONE = 0;
/**
* Hint layout to the side.
* @hide
*/
public static final int LAYOUT_HINT_SIDE = 1;
而現在的DatePickerDialog不支持使用這些值了,來看下它的構造方法:
public DatePickerDialog(@NonNull Context context, @StyleRes int themeResId, @Nullable OnDateSetListener listener, int year, int monthOfYear, int dayOfMonth) { this(context, themeResId, listener, null, year, monthOfYear, dayOfMonth); }看它的第二個參考限定爲StyleRes類型,只能使用style了。
二 DatePickerDialog樣式(自定義style)
1 NoActionBar
<style name="dialog_date" parent="Theme.AppCompat.NoActionBar">
<!--設置頭部顏色-->
<!--<item name="colorPrimary">@color/tip_text_color</item>-->
<!--<item name="colorPrimaryDark">@color/tip_text_color</item>-->
<!--<item name="colorAccent">@color/tip_text_color</item>-->
<!--設置dialog不全屏 -->
<item name="android:windowIsTranslucent">false</item>
<item name="android:windowNoTitle">true</item>
<!--除去title,如果在代碼裏面有其他設置會沒效果的不過完全沒效果-->
<item name="android:windowContentOverlay">@null</item>
<item name="android:backgroundDimEnabled">false</item>
<!--<!–除去背景色–>-->
<!--<item name="android:windowBackground">@null</item>-->
</style>
效果圖說明,此時也可以通過windowBackground屬性改變其背景顏色。
2 自定義樣式
這種樣式有浮動模式和非浮動模式,關鍵在於屬性windowIsFloating。
(1)非浮動模式
<style name="dialog_date">
<!--設置header部分顯示的顏色-->
<item name="colorPrimary">@color/tip_text_color</item>
<item name="colorPrimaryDark">@color/tip_text_color</item>
<item name="colorAccent">@color/tip_text_color</item>
<!--這個說明提示框是否有邊框-->
<item name="android:windowFrame">@null</item>
<!--這個說明提示框是否是浮動的 這個很重要-->
<item name="android:windowIsFloating">false</item>
<!--設置dialog不全屏 貌似沒有效果-->
<item name="android:windowFullscreen">false</item>
<!--這個說明提示框是滯是透明的-->
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<!--除去title,如果在代碼裏面有其他設置會沒效果的不過完全沒效果-->
<item name="android:windowContentOverlay">@null</item>
<item name="android:backgroundDimEnabled">false</item>
<!--<!–背景色 如果除去背景色 @null –>-->
<!--<item name="android:windowBackground">@color/red_F1</item>-->
<!--按鈕字體的顏色-->
<!--<item name="android:textColor">@color/dark_2B</item>-->
</style>
效果圖:
通過windowBackground屬性改變其背景顏色
(2)浮動模式
把上面windowIsFloating屬性設置爲true。
效果圖:
3 使用 Base.Theme.AppCompat.Light.Dialog.FixedSize
<style name="dialog_date" parent="Base.Theme.AppCompat.Light.Dialog.FixedSize">
<!--設置header部分顯示的顏色-->
<item name="colorPrimary">@color/tip_text_color</item>
<item name="colorPrimaryDark">@color/tip_text_color</item>
<item name="colorAccent">@color/tip_text_color</item>
</style>
效果圖:
說明,很明確這種樣式比起上面的多了個半透明背景,用戶體驗更好些,當然還有其他的很多樣式~,就不再這裏嘗試了。