UI太慢而无法充气,导致应用程序变慢

时间:2017-03-30 02:19:38

标签: android android-layout

我在ScrollView中有一个约束布局。此ConstraintLayout应该是将添加到其中的其他视图的父级,如下所示:

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

<android.support.constraint.ConstraintLayout
    android:id="@+id/generated_main_constraint"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</android.support.constraint.ConstraintLayout>

当我尝试向此ConstraintLayout添加视图时,我使用以下代码从XML中扩展视图:

(ConstraintLayout) getLayoutInflater().inflate(R.layout.box_npcs, MAIN_CONSTRAINT, false);

此布局在XML1中定义(问题的结尾)。

然后我将带有信息的TextViews添加到此Layout的LinearLayout中,因为我使用以编程方式创建的TextView并将其添加到布局中

linearLayout.addView(new TextView("Foo"));

通过该设置,我将此框添加到主要约束布局。

问题是:当我尝试添加超过5个视图时,需要很长时间来处理UI ,最多需要5秒钟才能添加最多视图(50 )。

如何以一种在尝试添加视图时不会滞后的方式将复杂视图添加到ConstraintLayout?

XML1:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/box_npcs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">


<ImageView
    android:id="@+id/npcImage"
    android:layout_width="88dp"
    android:layout_height="88dp"
    android:layout_marginBottom="0dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:scaleType="fitXY"
    app:layout_constraintBottom_toBottomOf="@+id/layoutInformation"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="@+id/layoutInformation"
    app:srcCompat="@drawable/camera" />

<android.support.constraint.ConstraintLayout
    android:id="@+id/layoutInformation"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:maxWidth="280dp"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toRightOf="@+id/npcImage"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <TextView
        android:id="@+id/top_priority"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="0dp"
        android:layout_marginTop="0dp"
        android:ellipsize="start"
        android:text="DummyTextIsDummy"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        android:textColor="@color/common_google_signin_btn_text_light_default"
        android:textSize="22sp"
        android:textStyle="bold"
        android:typeface="normal"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="HardcodedText" />

    <LinearLayout
        android:id="@+id/linearLayout_high_highest_normal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintLeft_toLeftOf="@+id/top_priority"
        app:layout_constraintTop_toBottomOf="@+id/top_priority" />
</android.support.constraint.ConstraintLayout>

图片示例: 每个框都是&#39;在此屏幕中是主要约束布局中的框。 每个TextLine都是添加到LinearLayout的不同TextView

1 个答案:

答案 0 :(得分:1)

您的测试设备中的Android版本是什么?

几天前,我发现了一个问题。

如果我在Android 5或6中使用两个或更多ConstraintLayouts,它会很慢。 当我使用Android Studio CPU Profiler时,我看到com.android.internal.policy.impl.phonewindow$decorview.onmeasure花了很多时间。

所以我尝试使用LinearLayout,问题解决了。

我认为支持库中的ConstraintLayout对Android 5或6不友好。

相关问题