代码之家  ›  专栏  ›  技术社区  ›  baxbear

如何以编程方式向滚动视图添加约束布局?

  •  0
  • baxbear  · 技术社区  · 6 年前

    我有以下想法:

    1. 创建一个新活动,该活动在布局的某个位置包含滚动视图
    2. 创建一个约束布局(宽度在“匹配父对象”上),每个对象旁边有一个编辑字段和一个文本视图

    =>现在我想用按钮之类的东西向滚动视图添加任意数量的此类约束布局。

    有人能解释一下这是怎么做到的吗?这条路有可能吗?

    (安德罗伊德工作室)

    编辑: 我尝试了以下方法:

    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);”测试时崩溃。 (感谢您已经提出的解决方案,一旦我理解我在这里所做的错误,将立即测试它…)

    2 回复  |  直到 6 年前
        1
  •  1
  •   Nirav Bhavsar    6 年前

    button textview edittext

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
    
        ConstraintLayout constraintLayout = findViewById(R.id.constraintlayout);
    
        // Create btn_contact_us1
        Button btn_contact_us1 = new Button(this);
        // Generate an Id and assign it to programmatically created Button
        btn_contact_us1.setId(View.generateViewId());
        btn_contact_us1.setText("Contact Us 1");
        btn_contact_us1.setLayoutParams(new ConstraintLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        // Add programmatically created Button to ConstraintLayout
        constraintLayout.addView(btn_contact_us1);
    
        // Create btn_contact_us2
        Button btn_contact_us2 = new Button(this);
        // Generate an Id and assign it to programmatically created Button
        btn_contact_us2.setId(View.generateViewId());
        btn_contact_us2.setText("Contact Us 2");
        btn_contact_us2.setLayoutParams(new ConstraintLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        // Add programmatically created Button to ConstraintLayout
        constraintLayout.addView(btn_contact_us2);
    
        // Create ConstraintSet
        ConstraintSet constraintSet = new ConstraintSet();
        // Make sure all previous Constraints from ConstraintLayout are not lost
        constraintSet.clone(constraintLayout);
    
        // Create Rule that states that the START of btn_contact_us1 will be positioned at the END of btn_contact_us2
        constraintSet.connect(btn_contact_us2.getId(), ConstraintSet.START, btn_contact_us1.getId(), ConstraintSet.END);
        constraintSet.applyTo(constraintLayout);    
    }
    

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView
        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"
        android:fillViewport="true">
    
        <android.support.constraint.ConstraintLayout
        android:id="@+id/constraintlayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    
    </ScrollView>
    

    check link image

        2
  •  0
  •   BHARATHWAJ    6 年前
    <?xml version="1.0" encoding="utf-8"?>
     <ScrollView android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
        <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=".MainActivity">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/B1"/>
    </android.support.constraint.ConstraintLayout> </ScrollView>