简体   繁体   English

在Android中创建自定义日历形状,例如Google日历图像

[英]create custom calendar shape like google calendar image in android

i want to create custom calendar shape like google calendar icon and use it as a container of other views like ImageView and Textview i try use XML drawable shape and the result seem not be good and i try to create it programmatically but i cant know how to put other views in custom view what is the proper way to create that? 我想创建自定义日历形状(如Google日历图标)并将其用作其他视图(如ImageView和Textview)的容器,我尝试使用XML可绘制形状,结果似乎效果不佳,我尝试以编程方式创建它,但我不知道如何将其他视图放入自定义视图中,创建该视图的正确方法是什么?

bg_calendar_view.xml bg_calendar_view.xml

<!-- Gray shadow. -->
<item>
    <shape>
        <solid android:color="#dadada" />
        <corners android:radius="15dp" />
    </shape>
</item>

<!-- White foreground.  -->
<item
    android:top="3dp"
    android:left="2dp"
    android:bottom="1dp"
    >
    <shape>
        <solid android:color="@android:color/white" />
        <corners android:radius="15dp" />
    </shape>
</item>

bg_notification_view.xml bg_notification_view.xml

<!-- Gray shadow. -->
<item>
    <shape>
        <solid android:color="#dadada" />
        <corners android:radius="15dp" />
    </shape>
</item>

<!-- White foreground.  -->
<item
    android:bottom="3dp"
    android:top="1dp"
    android:left="2dp">
    <shape>
        <solid android:color="@android:color/white" />
        <corners android:radius="15dp" />
    </shape>
</item>

bg_bottom_calendar.xml bg_bottom_calendar.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:bottom="4dp"
    android:right="25dp"
    android:left="25dp"
    android:drawable="@drawable/bg_notification_view">

</item>
<item
    android:left="15dp"
    android:bottom="20dp"
    android:right="15dp"


    android:drawable="@drawable/bg_notification_view">


</item>
<item
    android:bottom="40dp"
    android:drawable="@drawable/bg_notification_view">

</item>

bg_top_calendar.xml bg_top_calendar.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:bottom="4dp"
    android:right="25dp"
    android:left="25dp"
    android:drawable="@drawable/bg_calendar_view">

</item>
<item
    android:right="15dp"
    android:left="15dp"
    android:top="20dp"
    android:drawable="@drawable/bg_calendar_view">

</item>
<item
    android:top="40dp"
    android:drawable="@drawable/bg_calendar_view_blue">

</item>

my_layout.xml my_layout.xml

<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
android:padding="16dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:background="@drawable/bg_top_calendar"
    android:padding="40dp"
    android:gravity="center"
    android:orientation="vertical"
    >
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/notification"
        android:layout_gravity="center"
        android:layout_marginBottom="8dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="you Have 31 Notification"
        android:textColor="@color/white"
        android:textSize="18sp"/>

</LinearLayout>
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:background="@drawable/bg_bottom_calendar"
    android:paddingBottom="40dp"
    android:paddingStart="4dp"
    android:paddingEnd="4dp"
    android:paddingTop="4dp"
    >

</FrameLayout>

We developed a Calendar for few months. 我们开发了一个日历几个月。

First we used linear layout to populate cells. 首先,我们使用线性布局来填充单元格。 We had 35 cells and each cell contained 1 RelativeLayout and 3 TextViews. 我们有35个单元格, 每个单元格包含1个RelativeLayout和3个TextViews。

Then we populated views into cells programmatically, which was easy but turned out to be a very sluggish implementation. 然后我们以编程方式将视图填充到单元格中,这很简单,但事实证明这是一个非常缓慢的实现。 Then we used RecyclerView for cell implementation, but it didn't speedup by very margin. 然后我们使用RecyclerView进行单元实现,但是并没有非常快的速度。

Once We find out our bottleneck to be populating the complex views 35 times. 一旦我们发现瓶颈,需要填充复杂视图35次。

Then we came across a video about Custom Views. 然后,我们观看了有关自定义视图的视频 So we decided to give Custom Views a try after much study, and Voila! 因此,我们决定在大量研究后尝试使用“自定义视图”,瞧! It solved our lagging issues like a charm. 它像魅力一样解决了我们的滞后问题。

You should too give it a try like us. 您也应该像我们一样尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM