ConstraintSet.RIGHT无法正常工作

时间:2017-09-16 02:39:51

标签: android-constraintlayout

我正在尝试创建一个可以在没有数据库的情况下动态地将视图添加到ConstraintLayout的应用程序。我把LayoutParams包装在那里以查看它们的去向。我已经能够添加视图,但我不知道为什么他们会一直走到屏幕的左侧,尽管我为每个视图使用了不同的LayoutParams,但是大小也是一样的。边缘似乎也不起作用。以下是我的代码:

        ConstraintSet set = new ConstraintSet();
        ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(
                ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
        lp.setMargins(8, 8, 8, 0);
        ConstraintLayout.LayoutParams lpWrap = new ConstraintLayout.LayoutParams(
                ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
        int fieldCount = sharedPref.getInt(fieldCountTag, 0);

        fieldCount++;

        editor = sharedPref.edit();
        editor.putInt(fieldCountTag, fieldCount);
        editor.commit();

        EditText txt = new EditText(ctx);
        txt.setHint("IP:Port "+fieldCount);
        txt.setInputType(InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT);
        txt.setId(fieldCount);
        txt.setLayoutParams(lp);


        Button btn = new Button(ctx);
        btn.setText("Use "+fieldCount);
        btn.setId(fieldCount);
        btn.setLayoutParams(lpWrap);

        mainLyt.addView(txt);
        mainLyt.addView(btn);

        set.clone(mainLyt);

        set.connect(btn.getId(), ConstraintSet.RIGHT, mainLyt.getId(), ConstraintSet.RIGHT, 8);
        set.connect(btn.getId(), ConstraintSet.TOP, txt.getId(), ConstraintSet.TOP, 0);
        set.connect(txt.getId(), ConstraintSet.LEFT, mainLyt.getId(), ConstraintSet.LEFT, 8);
        set.connect(txt.getId(), ConstraintSet.TOP, txt3.getId(), ConstraintSet.BOTTOM, 8);
        set.connect(txt.getId(), ConstraintSet.RIGHT, btn.getId(), ConstraintSet.LEFT, 8);

        set.applyTo(mainLyt);

更新:

我想要做的只是简单地添加一个edittext和按钮

更新: 我目前的代码:

    ConstraintSet set = new ConstraintSet();
    ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(
            ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
    lp.setMargins(8, 8, 8, 0);
    ConstraintLayout.LayoutParams lpWrap = new ConstraintLayout.LayoutParams(
            ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
    int fieldCount = sharedPref.getInt(fieldCountTag, initFieldCount);
    int topViewId = 0;

    for(int x = 3; x < fieldCount; x++){
        if(txtId != 0){
            prevTxtId = txtId;
        }

        txtId = View.generateViewId();
        btnId = View.generateViewId();

        int id = x;
        id++;
        EditText txt = new EditText(ctx);
        txt.setHint("IP:Port "+id);
        txt.setInputType(InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT);
        txt.setId(txtId);
        txt.setLayoutParams(lp);

        Button btn = new Button(ctx);
        btn.setText("Use "+id);
        btn.setId(btnId);
        btn.setLayoutParams(lpWrap);

        mainLyt.addView(txt);
        mainLyt.addView(btn);

        set.clone(mainLyt);

        if(x == 3){
            Log.d(TAG, "txt3Id: "+txt3.getId());
            topViewId = txt3.getId();
        }else{
            topViewId = prevTxtId;
        }

        set.connect(btnId, ConstraintSet.END, mainLyt.getId(), ConstraintSet.END, 0);
        set.connect(btnId, ConstraintSet.TOP, txtId, ConstraintSet.TOP, 0);
        set.connect(txtId, ConstraintSet.START, mainLyt.getId(), ConstraintSet.START, 0);
        set.connect(txtId, ConstraintSet.TOP, topViewId, ConstraintSet.BOTTOM, 0);
        set.connect(txtId, ConstraintSet.END, btnId, ConstraintSet.START, 0);

        set.applyTo(mainLyt);
    }

更新: 我的加载方法:

        ConstraintSet set = new ConstraintSet();
        ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(
                ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
        lp.setMargins(8, 8, 8, 0);
        ConstraintLayout.LayoutParams lpWrap = new ConstraintLayout.LayoutParams(
                ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
        int fieldCount = sharedPref.getInt(fieldCountTag, initFieldCount);
        int topViewId = 0;
        Log.d(TAG, "fieldCount: "+fieldCount);
        Log.d(TAG, "------");

        for(int x = 3; x < fieldCount; x++){
            if(txtId != 0){
                prevTxtId = txtId;
            }

            txtId = View.generateViewId();
            btnId = View.generateViewId();

            int id = x;
            id++;
            EditText txt = new EditText(ctx);
            txt.setHint("IP:Port "+id);
            txt.setInputType(InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT);
            txt.setId(txtId);
            txt.setLayoutParams(lp);

            Button btn = new Button(ctx);
            btn.setText("Use "+id);
            btn.setId(btnId);
            btn.setLayoutParams(lpWrap);

            mainLyt.addView(txt);
            mainLyt.addView(btn);

            set.clone(mainLyt);

            if(x == 3){
                Log.d(TAG, "txt3Id: "+txt3.getId());
                topViewId = txt3.getId();
            }else{
                topViewId = prevTxtId;
            }

            Log.d(TAG, "txtId: "+txtId+" connect to topView: "+topViewId+" end"+" btnId: "+btnId);

            set.connect(btnId, ConstraintSet.END, mainLyt.getId(), ConstraintSet.END, 0);
            set.connect(btnId, ConstraintSet.TOP, txtId, ConstraintSet.TOP, 0);
            set.connect(txtId, ConstraintSet.START, mainLyt.getId(), ConstraintSet.START, 0);
            set.connect(txtId, ConstraintSet.TOP, topViewId, ConstraintSet.BOTTOM, 0);
            set.connect(txtId, ConstraintSet.END, btnId, ConstraintSet.START, 0);

            prevTxtId = txtId;

            set.applyTo(mainLyt);
        }

更新:

我尝试在xml中使用3个EditText连接到彼此,它运行正常。我的加载方法的逻辑是相同的仍然不起作用。观点一直持续到顶部。日志显示

txtId: 1 connect to topView: 2131558527 end btnId: 2 fieldCount: 4
txtId: 3 connect to topView: 1 end btnId: 4 fieldCount: 5

哪个应该是正确的。

更新:

如果你想尝试的话,这是我的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/mainLyt"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="app.com.simplewebview.MainActivity">

    <EditText
        android:id="@+id/txt1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="IP:Port 1"
        android:inputType="textUri"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/btn1"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:layout_marginTop="0dp"
        android:text="Use 1"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/txt1" />

    <EditText
        android:id="@+id/txt2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="IP:Port 2"
        android:inputType="textUri"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/btn2"
        app:layout_constraintTop_toBottomOf="@+id/txt1" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:layout_marginTop="0dp"
        android:text="Use 2"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/txt2" />

    <EditText
        android:id="@+id/txt3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="IP:Port 3"
        android:inputType="textUri"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/btn3"
        app:layout_constraintTop_toBottomOf="@+id/txt2" />

    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:layout_marginTop="0dp"
        android:text="Use 3"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/txt3" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginRight="8dp"
        android:clickable="true"
        app:fabSize="mini"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:srcCompat="@mipmap/ic_launcher" />

</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

txtbtn需要有不同的ID或者约束不起作用。如果您的View.generateViewId()为17或更高,请尝试使用minSdkVersion。无论你做什么,你的观点都需要不同的ID。

确定您设置的ID不同后,请检查您的约束。这些看起来还不错,但是在txt3有垂直约束之前,不清楚容器中的垂直约束是什么。另外,如果仍有问题,请尝试开始/结束而不是左/右。

可能还有其他一些事情发生,但是进行这些更改后,您应该会看到一些问题消失。