我有以下想法:
-
创建一个新活动,该活动在布局的某个位置包含滚动视图
-
创建一个约束布局(宽度在“匹配父对象”上),每个对象旁边有一个编辑字段和一个文本视图
=>现在我想用按钮之类的东西向滚动视图添加任意数量的此类约束布局。
有人能解释一下这是怎么做到的吗?这条路有可能吗?
(安德罗伊德工作室)
编辑:
我尝试了以下方法:
protected void addElementToScrollView() {
ScrollView sv = getLayoutInflater()
.inflate(R.layout.activity_goods_received_separation_on_container_level, null)
.findViewById(R.id.scrollViewChanges);
ConstraintLayout cl = findViewById(R.id.gc_scrollview_element);
sv.addView(cl);
}
这在包含滚动视图的活动中:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GoodsReceived_Separation_On_ContainerLevel">
<TextView
android:id="@+id/goods_received_num"
android:layout_width="73dp"
android:layout_height="27dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.601"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/goods_received_mat2" />
<TextView
android:id="@+id/goods_received_mat2"
android:layout_width="73dp"
android:layout_height="27dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.601"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="73dp"
android:layout_height="27dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="@string/goods_received_num"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
<TextView
android:id="@+id/textView7"
android:layout_width="120dp"
android:layout_height="27dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="@string/goods_received_eme"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/divider" />
<TextView
android:id="@+id/textView8"
android:layout_width="120dp"
android:layout_height="27dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="@string/goods_received_loc"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView10" />
<TextView
android:id="@+id/textView10"
android:layout_width="120dp"
android:layout_height="27dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="@string/goods_received_type"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView9" />
<TextView
android:id="@+id/textView9"
android:layout_width="120dp"
android:layout_height="27dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="@string/goods_received_bme"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView7" />
<TextView
android:id="@+id/textView3"
android:layout_width="73dp"
android:layout_height="27dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="@string/goods_received_mat"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/divider"
android:layout_width="368dp"
android:layout_height="3dp"
android:layout_marginTop="8dp"
android:background="?android:attr/listDivider"
android:divider="#000000"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/goods_received_num" />
<ScrollView
android:id="@+id/scrollViewChanges"
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_marginTop="48dp"
android:fillViewport="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView8">
</ScrollView>
<Button
android:id="@+id/button_apply_gr_change"
android:layout_width="137dp"
android:layout_height="68dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/scrollViewChanges" />
<TextView
android:id="@+id/textView11"
android:layout_width="100dp"
android:layout_height="26dp"
android:layout_marginBottom="4dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/goods_received_loc"
app:layout_constraintBottom_toTopOf="@+id/scrollViewChanges"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView13"
app:layout_constraintTop_toBottomOf="@+id/textView8"
app:layout_constraintVertical_bias="0.2" />
<TextView
android:id="@+id/textView13"
android:layout_width="100dp"
android:layout_height="26dp"
android:layout_marginBottom="4dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/goods_received_type"
app:layout_constraintBottom_toTopOf="@+id/scrollViewChanges"
app:layout_constraintEnd_toStartOf="@+id/textView11"
app:layout_constraintStart_toEndOf="@+id/textView12"
app:layout_constraintTop_toBottomOf="@+id/textView8"
app:layout_constraintVertical_bias="0.2" />
<TextView
android:id="@+id/textView12"
android:layout_width="100dp"
android:layout_height="26dp"
android:layout_marginBottom="4dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/goods_received_eme"
app:layout_constraintBottom_toTopOf="@+id/scrollViewChanges"
app:layout_constraintEnd_toStartOf="@+id/textView13"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView8"
app:layout_constraintVertical_bias="0.222" />
<View
android:id="@+id/divider3"
android:layout_width="368dp"
android:layout_height="1dp"
android:layout_marginBottom="4dp"
android:layout_marginTop="4dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toTopOf="@+id/textView11"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView8" />
</android.support.constraint.ConstraintLayout>
这是我试图添加到滚动视图中的布局元素(任何次数,所以在滚动视图中可以同时拥有其中的10个,并且数字应该是动态递增的)
<?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/gc_scrollview_element"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:id="@+id/spinner"
android:layout_width="100dp"
android:layout_height="27dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/spinner3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Spinner
android:id="@+id/spinner3"
android:layout_width="100dp"
android:layout_height="27dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/spinner2"
app:layout_constraintStart_toEndOf="@+id/spinner"
app:layout_constraintTop_toTopOf="parent" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="100dp"
android:layout_height="27dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/spinner3"
app:layout_constraintTop_toTopOf="parent" />
(我知道它包含的元素与建议的问题不同,但我认为问题没有区别。)
我的解决方案在“sv.addview(cl);”测试时崩溃。
(感谢您已经提出的解决方案,一旦我理解我在这里所做的错误,将立即测试它…)