Android: Material Date Picker自定义样式

huangapple go评论58阅读模式
英文:

Android: Material Date Picker Custom Styling

问题

我正在在我的Android应用中使用Material-Component日期范围选择器,并希望自定义标题布局。我尝试在自定义样式中添加materialCalendarHeaderLayout项,但由于我是Android的新手,不确定如何正确使用它。

  1. 我想隐藏标题。
  2. 想要自定义年份和月份按钮。 (可选任务)

我使用themes.xml来修改日期选择器,使其以弹出窗口而不是全屏显示。

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- 基本应用主题 -->
    <style name="Theme.MyApp" parent="Theme.MaterialComponents.Light.NoActionBar">
        <!-- 主要品牌颜色 -->
        <item name="colorPrimary">@color/orange</item>
        <item name="colorPrimaryVariant">@color/black</item>
        <item name="colorOnPrimary">@color/black</item>
        <!-- 次要品牌颜色 -->
        <item name="colorSecondary">@color/orange</item>
        <item name="colorSecondaryVariant">@color/black</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- 状态栏颜色 -->
        <item name="android:statusBarColor">@color/black</item>
        <!-- 日期范围选择器 -->
        <item name="materialCalendarFullscreenTheme">@style/CustomThemeOverlay_MaterialCalendar_Fullscreen</item>
    </style>

    <!-- 弹出菜单主题 -->
    <style name="PopupMenuBlackBackground" parent="ThemeOverlay.AppCompat.Dark">
        <item name="android:popupMenuStyle">@style/PopupMenuBlackBackground.Menu</item>
        <item name="android:fontFamily">@font/poppins_regular</item>
        <item name="android:popupElevation">5dp</item>
        <item name="android:radius">5dp</item>
    </style>
    <style name="PopupMenuBlackBackground.Menu" parent="Widget.AppCompat.PopupMenu">
        <item name="android:popupBackground">@color/blackDark</item>
        <item name="android:popupElevation">5dp</item>
        <item name="android:radius">5dp</item>
    </style>

    <!-- 日期范围选择器全屏 -->
    <style name="CustomThemeOverlay_MaterialCalendar_Fullscreen" parent="@style/ThemeOverlay.MaterialComponents.MaterialCalendar.Fullscreen">
        <item name="materialCalendarStyle">@style/Custom_MaterialCalendar.Fullscreen</item>
    </style>
    <style name="Custom_MaterialCalendar.Fullscreen" parent="@style/Widget.MaterialComponents.MaterialCalendar.Fullscreen">
        <item name="android:windowFullscreen">false</item>
    </style>
</resources>

期望的日期范围选择器界面如下:

Android: Material Date Picker自定义样式

可以有人指导我如何实现这种自定义吗?对于任何帮助或建议,我将不胜感激。
提前感谢您的帮助。

英文:

I am using the Material-Component Date Range Picker in my Android app and I want to customize the header layout. I have tried adding the materialCalendarHeaderLayout item in my custom style, but I am not sure how to use it properly as I am new on the Android.

  1. I want to hide the header.
  2. Want a custom Year & Month button. (Optional task)

Android: Material Date Picker自定义样式

I used the themes.xml to modify the date picker to appear as a popup instead of fullscreen.

&lt;resources xmlns:tools=&quot;http://schemas.android.com/tools&quot;&gt;
&lt;!-- Base application theme. --&gt;
&lt;style name=&quot;Theme.MyApp&quot; parent=&quot;Theme.MaterialComponents.Light.NoActionBar&quot;&gt;
&lt;!-- Primary brand color. --&gt;
&lt;item name=&quot;colorPrimary&quot;&gt;@color/orange&lt;/item&gt;
&lt;item name=&quot;colorPrimaryVariant&quot;&gt;@color/black&lt;/item&gt;
&lt;item name=&quot;colorOnPrimary&quot;&gt;@color/black&lt;/item&gt;
&lt;!-- Secondary brand color. --&gt;
&lt;item name=&quot;colorSecondary&quot;&gt;@color/orange&lt;/item&gt;
&lt;item name=&quot;colorSecondaryVariant&quot;&gt;@color/black&lt;/item&gt;
&lt;item name=&quot;colorOnSecondary&quot;&gt;@color/black&lt;/item&gt;
&lt;!-- Status bar color. --&gt;
&lt;item name=&quot;android:statusBarColor&quot;&gt;@color/black&lt;/item&gt;
&lt;!-- Date range picker --&gt;
&lt;item name=&quot;materialCalendarFullscreenTheme&quot;&gt;@style/CustomThemeOverlay_MaterialCalendar_Fullscreen&lt;/item&gt;
&lt;/style&gt;
&lt;!-- Popup Menu theme --&gt;
&lt;style name=&quot;PopupMenuBlackBackground&quot; parent=&quot;ThemeOverlay.AppCompat.Dark&quot;&gt;
&lt;item name=&quot;android:popupMenuStyle&quot;&gt;@style/PopupMenuBlackBackground.Menu&lt;/item&gt;
&lt;item name=&quot;android:fontFamily&quot;&gt;@font/poppins_regular&lt;/item&gt;
&lt;item name=&quot;android:popupElevation&quot;&gt;5dp&lt;/item&gt;
&lt;item name=&quot;android:radius&quot;&gt;5dp&lt;/item&gt;
&lt;/style&gt;
&lt;style name=&quot;PopupMenuBlackBackground.Menu&quot; parent=&quot;Widget.AppCompat.PopupMenu&quot;&gt;
&lt;item name=&quot;android:popupBackground&quot;&gt;@color/blackDark&lt;/item&gt;
&lt;item name=&quot;android:popupElevation&quot;&gt;5dp&lt;/item&gt;
&lt;item name=&quot;android:radius&quot;&gt;5dp&lt;/item&gt;
&lt;/style&gt;
&lt;!-- Date range picker fullscreen --&gt;
&lt;style name=&quot;CustomThemeOverlay_MaterialCalendar_Fullscreen&quot;
parent=&quot;@style/ThemeOverlay.MaterialComponents.MaterialCalendar.Fullscreen&quot;&gt;
&lt;item name=&quot;materialCalendarStyle&quot;&gt;@style/Custom_MaterialCalendar.Fullscreen&lt;/item&gt;
&lt;/style&gt;
&lt;style name=&quot;Custom_MaterialCalendar.Fullscreen&quot;
parent=&quot;@style/Widget.MaterialComponents.MaterialCalendar.Fullscreen&quot;&gt;
&lt;item name=&quot;android:windowFullscreen&quot;&gt;false&lt;/item&gt;
&lt;/style&gt;
&lt;/resources&gt;

Expexted Date range picker:

Android: Material Date Picker自定义样式

Can someone guide me on how to achieve this customization? Any help or suggestions would be appreciated.
Thank you in advance.

答案1

得分: 3

不显示页眉布局,您可以使用以下代码:

<style name="App.MaterialCalendarTheme" parent="ThemeOverlay.Material3.MaterialCalendar">
    <item name="materialCalendarHeaderLayout">@style/App.Material3.MaterialCalendar.HeaderLayout</item>
</style>

<style name="App.Material3.MaterialCalendar.HeaderLayout" parent="@style/Widget.Material3.MaterialCalendar.HeaderLayout">
    <item name="android:visibility">gone</item>
</style>

Android: Material Date Picker自定义样式

英文:

To hide the header layout you can use:

&lt;style name=&quot;App.MaterialCalendarTheme&quot; parent=&quot;ThemeOverlay.Material3.MaterialCalendar&quot;&gt;
&lt;item name=&quot;materialCalendarHeaderLayout&quot;&gt;@style/App.Material3.MaterialCalendar.HeaderLayout&lt;/item&gt;
&lt;/style&gt;
&lt;style name=&quot;App.Material3.MaterialCalendar.HeaderLayout&quot; parent=&quot;@style/Widget.Material3.MaterialCalendar.HeaderLayout&quot;&gt;
&lt;item name=&quot;android:visibility&quot;&gt;gone&lt;/item&gt;
&lt;/style&gt;

Android: Material Date Picker自定义样式

huangapple
  • 本文由 发表于 2023年2月14日 22:10:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75449042.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定