如何在约束布局内的循环中插入动态生成的文本视图?

时间:2019-04-04 06:28:16

标签: android android-layout android-constraintlayout

我试图以编程方式在约束布局内的循环中包含多个视图。我该怎么做?

我想要这样的东西:
I want something like this

我可以对单个视图执行此操作,但是我无法对循环内的多个视图执行相同的操作。基本上,我有一个滚动视图,并且在其中有两行,在行的顶部和底部,我想在这些行之间包含以编程方式创建的文本视图。我提供了以下代码行:

final TextView[] myTextViews = new TextView[3];
        int i=1;
        ConstraintSet constraintSet = new ConstraintSet();
        myTextViews[i-1] = new TextView(this);
        myTextViews[i-1].setId(R.id.testid);
        myTextViews[i-1].setText("Text 1");
        ConstraintLayout.LayoutParams test = new ConstraintLayout.LayoutParams(
                ConstraintLayout.LayoutParams.MATCH_CONSTRAINT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
        myTextViews[i-1].setLayoutParams(test);
        constraintLayout.addView(myTextViews[i-1]);
        constraintSet.clone(constraintLayout);
        constraintSet.connect(myTextViews[i-1].getId(), ConstraintSet.TOP, lineTop.getId(), ConstraintSet.BOTTOM, 28);
        constraintSet.connect(myTextViews[i-1].getId(), ConstraintSet.LEFT, constraintLayout.getId(), ConstraintSet.LEFT, 70);
        constraintSet.connect(myTextViews[i-1].getId(), ConstraintSet.RIGHT, constraintLayout.getId(), ConstraintSet.RIGHT, 70);

        constraintSet.applyTo(constraintLayout);
        for ( i = 1; i < 3; i++) {
            myTextViews[i] = new TextView(this);
            myTextViews[i].setId(R.id.testid+i);
            myTextViews[i].setText("Hello World "+i);
            myTextViews[i].setLayoutParams(test);
            constraintLayout.addView(myTextViews[i]);
            constraintSet.connect(myTextViews[i].getId(), ConstraintSet.TOP, myTextViews[i-1].getId(), ConstraintSet.BOTTOM, 28);
            constraintSet.connect(myTextViews[i].getId(), ConstraintSet.LEFT, constraintLayout.getId(), ConstraintSet.LEFT, 76);
            constraintSet.connect(myTextViews[i].getId(), ConstraintSet.RIGHT, constraintLayout.getId(), ConstraintSet.RIGHT, 76);
            constraintSet.applyTo(constraintLayout);
        }
        constraintSet.connect(lineBottom.getId(), ConstraintSet.TOP, myTextViews[i-1].getId(), ConstraintSet.BOTTOM, 8);
        constraintSet.applyTo(constraintLayout);

以下是xml代码:

<?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/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.reddevil.practice.practiceActivity">


    <ScrollView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="184dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="72dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

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

            <TextView
                android:id="@+id/text1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@drawable/textgps"
                android:text="Random Text"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="parent" />
           <TextView
                android:id="@+id/lineTop"
                android:layout_width="0dp"
                android:layout_height="0dp"    
                android:text=" "
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/dropoff" />

            <TextView
                android:id="@+id/forstops"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:text=" "
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="1.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/lineTop" />
           <TextView
                android:id="@+id/text2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@drawable/textgps"
                android:text="Random Text"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="parent" />

        </android.support.constraint.ConstraintLayout>
    </ScrollView>
</android.support.constraint.ConstraintLayout>

2 个答案:

答案 0 :(得分:0)

您可以做的是制作一个可重用的组件(根据您共享的图像,一个自定义的文本视图)。在主xml文件中,可以使用ScrollView内的ListView或RecyclerView来利用该可重用组件。

答案 1 :(得分:0)

对于这种用法,您应该使用RecyclerView或ListView。

如果要与之一起工作,则必须稍作更新

第一件事:不要使用约束布局(ID为scroll的约束布局,应将其替换为属性为LinearLayout的{​​{1}}

您将拥有一个看起来像这样的xml布局(我仅使用重要属性简化了它,但您也需要其他属性)

android:orientation="vertical"

然后,为您添加TextViews:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout >
    <ScrollView
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:id="@+id/scroll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/text1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/textgps"
                android:text="Random Text"/>
            <View style="@style/Divider"/>

            <LinearLayout
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"/>

            <View style="@style/Divider"/>
            <TextView
                android:id="@+id/text2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@drawable/textgps"
                android:text="Random Text"/>

        </LinearLayout>
    </ScrollView>
</android.support.constraint.ConstraintLayout>

但是您的textView格式没有太多...

您可以为添加的每一行创建一个xml布局文件,以便可以轻松自定义它:

final TextView[] myTextViews = new TextView[3];
LinearLayout container = findViewById(R.id.container);

LinearLayout .LayoutParams test = new LinearLayout .LayoutParams(
            LinearLayout.LayoutParams.MATCH_CONSTRAINT, LinearLayout.LayoutParams.WRAP_CONTENT);

for ( i = 1; i < 3; i++) {
        myTextViews[i] = new TextView(this);
        //i don't think you should do that
        //myTextViews[i].setId(R.id.testid+i);
        myTextViews[i].setText("Hello World "+i);
        myTextViews[i].setLayoutParams(test);
        container.addView(myTextViews[i]);
}

代码如下:

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

    <TextView
        android:id="@+id/text_row_text"
        android:layout_width="0px"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:textSize="@dimen/text_size_data"
        android:layout_marginLeft="15dp"
        android:layout_marginStart="15dp"/>
    <Button
        android:id="@+id/text_row_delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorError"
        android:text="X"/>
</LinearLayout>

如果您没有太多可以显示的项目,可以这样做,但是不要尝试像这样列出数百个视图,这对于表演来说很糟糕