Android CalendarView减慢了布局

car*_*o11 17 performance android android-layout android-xml calendarview

这真让我抓狂.我的Android应用程序中有一个片段,它使用RelativeLayout进行布局.问题是由于某种原因需要花费很长时间才能渲染,大约5秒钟就可以在屏幕上加载大约4个元素.

其中一个元素是CalendarView,当我删除它时,它会恢复到正常速度(即:即时).我想这个问题与我要求Android公布它的方式有关,一定不是很有意义,也不是效率低下的东西.

这是XML文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">

<TextView
     android:id="@+id/title"
     android:layout_width="0dp"
     android:layout_height="wrap_content"
     android:textStyle="bold"
     android:text="TODAY"
     android:textAppearance="?android:attr/textAppearanceLarge" 

     android:layout_alignParentLeft="true"
     android:layout_alignParentTop="true"
     android:layout_toLeftOf="@+id/time" />

<TextView
     android:id="@id/time"
     android:gravity="right"
     android:textStyle="bold"
     android:layout_width="100dp"
     android:layout_height="wrap_content"
     android:text="11:32"
     android:textAppearance="?android:attr/textAppearanceLarge"

     android:layout_alignParentRight="true" />

<TextView
    android:id="@+id/date_info"
    android:layout_margin="20dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Wednesday, 15th August 2012"
    android:textAppearance="?android:attr/textAppearanceMedium"

    android:layout_below="@id/title"
    android:layout_alignParentLeft="true" />

<CalendarView        
    android:id="@+id/calendar_view"
    android:layout_width="250dp"
    android:layout_height="250dp"
    android:background="@drawable/white_back_black_border"

    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我已经尝试了大量不同的布局选项,但删除日历似乎是唯一有所作为的东西.

任何见解将不胜感激.谢谢

Sal*_*lab 6

我也遇到过这个问题,包括相对布局和线性布局.它似乎与布局无关.

这是一个看错误的例子,只是简单的布局,根本没有代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".FechaHoraActivity" >

    <TimePicker
        android:id="@+id/timePicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp" />

    <CalendarView
        android:id="@+id/calendarView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

现在只需将线性布局高度更改为:

android:layout_height="match_parent"
Run Code Online (Sandbox Code Playgroud)

有了这个改变,问题就消失了,至少在我的三星Galaxy Tab 2中

所以它似乎与"空间"相关,但我仍然不确定为什么会出现这个问题.而且我没有在谷歌搜索"calendarview slow"中找到任何其他有趣的结果......

编辑 让我们看看我们是否能找到一个小小的赏金答案


Eve*_*ett 5

我花了几个小时才找到答案,但没弄清楚原因.这有点奇怪,可以帮助你找到答案.

CalendarView缓慢时的cpu消耗 在此输入图像描述

WeekAdapter中的getView具有从未使用过的param父级.

   public View getView(int position, View convertView, ViewGroup parent) {
       WeekView weekView = null;
        if (convertView != null) {
            weekView = (WeekView) convertView;
        } else {
            weekView = new WeekView(mContext);
            android.widget.AbsListView.LayoutParams params =
                new android.widget.AbsListView.LayoutParams(LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT);
            weekView.setLayoutParams(params);
            weekView.setClickable(true);
            weekView.setOnTouchListener(this);
        }

        int selectedWeekDay = (mSelectedWeek == position) ? mSelectedDate.get(
                Calendar.DAY_OF_WEEK) : -1;
        weekView.init(position, selectedWeekDay, mFocusedMonth);

        return weekView;
    }
Run Code Online (Sandbox Code Playgroud)

期待着答案.