【问题标题】:UI too slow to be inflated, causing application to be slowUI太慢无法膨胀,导致应用程序变慢
【发布时间】:2017-08-23 18:13:05
【问题描述】:

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

<?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 添加到此布局的 LinearLayout,为此我使用以编程方式创建的 TextView 并将其添加到布局中

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

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

问题是:当我尝试添加超过 5 个视图时,处理 UI 需要很长时间,要添加的最大视图量(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>

图片示例: 此屏幕中的每个“框”都是主约束布局中的一个框。 每个 TextLine 都是添加到 LinearLayout 的不同 TextView

【问题讨论】:

  • 您应该显示您的adding views 代码。缓慢的进度是没有添加视图进度。

标签: android android-layout


【解决方案1】:

你的测试设备的安卓版本是多少?

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

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

于是我尝试使用LinearLayout,问题解决了。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    • 2016-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多