android – UI太慢而无法充气,导致应用程序变慢
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了android – UI太慢而无法充气,导致应用程序变慢,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3660字,纯文字阅读大概需要6分钟。
内容图文
我在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>
图片例如:
此屏幕中的每个“框”都是主要约束布局中的一个框.
每个TextLine都是添加到LinearLayout的不同TextView
解决方法:
你的测试设备中的android版本是什么?
几天前,我发现了一个问题.
如果我在Android 5或6中使用两个或更多ConstraintLayouts,它将会很慢.
当我使用Android Studio CPU Profiler时,我看到com.android.internal.policy.impl.phonewindow $decorview.onmeasure花了很多时间.
所以我尝试使用LinearLayout,问题解决了.
我认为支持库中的ConstraintLayout对Android 5或6不友好.
内容总结
以上是互联网集市为您收集整理的android – UI太慢而无法充气,导致应用程序变慢全部内容,希望文章能够帮你解决android – UI太慢而无法充气,导致应用程序变慢所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。