日历视图显示需要很长时间

4

我正在开发一个日历视图的应用程序。

我需要在一个小的线性布局中显示日历视图。

当在一个小的线性布局中显示包含日历视图的整个页面时,会出现问题。

显示这个页面需要10秒钟的时间,这太长了。

布局中没有其他东西。

以下是我的xml代码和快照...

非常感谢帮助!

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:orientation="vertical" >

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="30"
                android:paddingLeft="30dp" >

                <TextView
                    android:id="@+id/txtDate"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="My Calendar"
                    android:textSize="50sp" />

                <View
                    android:layout_width="fill_parent"
                    android:layout_height="2dp"
                    android:layout_alignParentBottom="true"
                    android:layout_marginBottom="40dp"
                    android:layout_marginRight="30dp"
                    android:background="@android:color/black" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="70"
                android:paddingBottom="30dp"
                android:paddingLeft="20dp"
                android:paddingRight="20dp"
                android:paddingTop="30dp" >

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:orientation="vertical" >

                    <ListView
                        android:id="@+id/list_task"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content" >
                    </ListView>
                </LinearLayout>
            </RelativeLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:orientation="vertical" >

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="30" >

                <CalendarView
                    android:id="@+id/cal_small"
                    android:layout_width="250dp"
                    android:layout_height="250dp"
                    android:layout_centerHorizontal="true"
                    android:background="@android:color/darker_gray"
                    android:showWeekNumber="false"
                    android:animateLayoutChanges="false"
                    android:clipChildren="false"
                    android:drawingCacheQuality="low"
                    android:soundEffectsEnabled="false"
                    android:hapticFeedbackEnabled="false"
                     />

                <View
                    android:layout_width="fill_parent"
                    android:layout_height="2dp"
                    android:layout_alignParentBottom="true"
                    android:layout_marginBottom="40dp"
                    android:layout_marginLeft="30dp"
                    android:layout_marginRight="30dp"
                    android:background="@android:color/black" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="70" >

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_marginBottom="30dp"
                    android:layout_marginRight="10dp"
                    android:layout_marginTop="30dp"
                    android:orientation="vertical" >

                    <LinearLayout
                        android:layout_width="fill_parent"
                        android:layout_height="0dp"
                        android:layout_weight="8" >

                        <ListView
                            android:id="@+id/list1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:paddingLeft="30dp" >
                        </ListView>
                    </LinearLayout>

                    <ListView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" >
                    </ListView>
                </LinearLayout>
            </RelativeLayout>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

enter image description here


3
相关:https://dev59.com/WGYr5IYBdhLWcg3wRoOW - laalto
4个回答

10

看起来很简单,但我意外地测试了一下,它运行良好。希望这也能帮到您……

首先创建一个仅包含CalendarView的独立xml文件,命名为“cal_view.xml”。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <android.widget.CalendarView
        android:layout_width="200dp"
        android:layout_height="200dp" />

</LinearLayout> 

然后将这个xml文件包含在你的主xml文件中,就像下面这样。 用这个xml文件代替你的CalanderView。

 <include
     android:id="@+id/subCategory"
     layout="@layout/cal_view" />

1
我不知道它是如何工作的,但它对我来说非常有效...难以置信,@Alpan,你知道它是如何以惊人的性能工作的吗??? - Rumit Patel
这真是太棒了。将其放入LinearLayout中就解决了问题。 - Nestor Ledon

1

以上答案对我都不起作用,我只是将宽度和高度更改为“fill_parent”,现在一切正常。

<CalendarView
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"/>

奇怪的是,“fill_parent”和“match_parent”应该是相同的,但尚未测试。 - Rumit Patel

1
当你提到你的问题时,
问题:
你的资源xml文件(布局)不是最佳方式。由于Android指南不建议为ViewGroup使用静态高度和宽度。 请阅读http://developer.android.com/training/improving-layouts/optimizing-layout.html 解决方案:
我认为,您应该为创建日历视图创建自己的类。在其中应用所有属性和属性。
我展示了一个演示应用程序,用于Calender视图。http://www.androidviews.net/2013/01/ics-calendarview/ 在这些应用程序中,有更好的方式使用日历视图。我还提到了一些其他自定义日历视图的Git URL。 在上面的应用程序中,展示了使用日历视图的正确方式。
这里我展示了Calenderview应用程序的截图。

enter image description here enter image description here enter image description here

如果有任何问题,请告诉我,我会为您创建相应的示例演示。


你的回答可能对Dwivedi先生有用,但我不打算使用自定义日历..... - Rumit Patel
这里我只想展示如何正确使用日历视图。请看这个例子...https://github.com/SimonVT/android-calendarview。这是一个使用内置日历视图的最佳实践示例。 - Ashish Dwivedi
感谢@Dwivedi,它帮助我了解如何使用CalendarView。 但是像Alpan的解决方案一样,稍微改变一下,我的工作就完成了... - Rumit Patel

0
将CalendarView放入FrameLayout中可以解决问题。我测试过了,它完美地工作而没有任何故障!
这是一个XML代码示例:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#cc00b1cc">

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="102dp"
        android:layout_alignParentBottom="true">

        <CalendarView
            android:layout_width="425dp"
            android:layout_height="374dp"
            android:id="@+id/calendarView"
            android:layout_gravity="left|top" />
    </FrameLayout>
</RelativeLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接