【发布时间】: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代码。缓慢的进度是没有添加视图进度。