如何使用ConstraintLayout动态创建视图? (不工作)

时间:2018-05-04 10:43:05

标签: java android android-studio android-constraintlayout

我已经看到您可以使用LinearLayout动态创建小部件。但是,LinearLayout并没有一种简单的方法来相对于彼此约束新的小部件。

我找到了一个很好的例子here,表明我可以使用 constraintLayout.addView(widget)生成新的小部件。 但是,每次我在我的代码中尝试它时,它都会在我应用格式时杀死我的应用程序。

即。 constraintSet.applyTo(constraintLayout)杀死该应用。 但它只会杀死应用程序,因为constraintLayout。addView(小部件)不起作用。 (注释掉它们,代码运行正常,当然没有新的小部件)

使用LinearLayout我会得到新的小部件,但格式化是等等。

这是我的.java活动

public class GuestList extends AppCompatActivity implements View.OnClickListener, TextWatcher{

    int counter = 0;
    EditText et,editTextNextGuest,editTextGuestName,editTextGuestNum;
    CheckBox checkBoxRSVP;
    **ConstraintLayout cl;
    ConstraintSet cs;**
    LinearLayout layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guest_list);
        cl = (ConstraintLayout) findViewById(R.id.guestListMain);

        cs = new ConstraintSet();
        cs.clone(cl);

        et = (EditText) findViewById(R.id.guestName);
        et.setOnClickListener(this);
        et.addTextChangedListener( this);

    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        if (counter == 0) {

            layout = (LinearLayout) findViewById(R.id.layout1);

            editTextGuestName = findViewById(R.id.guestName);

            editTextNextGuest = new EditText(this);
            editTextNextGuest.setEms(10);
            editTextNextGuest.setHint("Next Guest");
            editTextNextGuest.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);

            editTextGuestNum = new EditText(this);
            editTextGuestNum.setEms(2);
            editTextGuestNum.setHint("#");
            editTextGuestNum.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);

            checkBoxRSVP = new CheckBox(this);

            // I don't want to use linear layout because ConstraintLayout has better methods (connect, setMargin, etc.)
            layout.addView(editTextNextGuest);
            layout.addView(editTextGuestNum);
            layout.addView(checkBoxRSVP);

            **//These are the issue, I want to use them
            //This link-> http://www.uwanttolearn.com/android/constraint-layout-animations-dynamic-constraints-ui-java-hell/
            //Shows them working, but I can't get them to work.
//            cl.addView(editTextNextGuest);
//            cl.addView(editTextGuestNum);
//            cl.addView(checkBoxRSVP);

            cs.setMargin(editTextNextGuest.getId(),ConstraintSet.START,16);
            cs.connect(editTextNextGuest.getId(),ConstraintSet.TOP,editTextGuestName.getId(),ConstraintSet.BOTTOM);
            cs.connect(editTextNextGuest.getId(),ConstraintSet.END,editTextGuestNum.getId(),ConstraintSet.START);
            cs.connect(editTextGuestNum.getId(),ConstraintSet.END,checkBoxRSVP.getId(),ConstraintSet.START);
            cs.connect(checkBoxRSVP.getId(),ConstraintSet.END,ConstraintSet.PARENT_ID,ConstraintSet.END);

            Log.d("tag", "It gets here");
            cs.applyTo(cl);**

            counter++;
        }
    }

这是我的.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/guestListMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".GuestList">

    <LinearLayout
        android:id="@+id/layout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></LinearLayout>
    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginTop="20dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/rsvpTitle" />

    <EditText
        android:id="@+id/guestName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:hint="Guest Name"
        android:inputType="textPersonName"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/guestTitle" />

0 个答案:

没有答案