Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用ConstraintLayout动态创建视图?(不工作)_Java_Android_Android Studio_Android Constraintlayout - Fatal编程技术网

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

Java 如何使用ConstraintLayout动态创建视图?(不工作),java,android,android-studio,android-constraintlayout,Java,Android,Android Studio,Android Constraintlayout,我已经看到,您可以使用LinearLayout动态创建小部件。但是,LinearLayout没有一种简单的方法来约束新的小部件 我发现了一个很好的例子,表明我可以使用 constraintLayout.addView(小部件)生成新的小部件。 但是,每次我在代码中尝试它时,当我应用格式时,它就会杀死我的应用程序 例如,constraintSet.applyTo(constraintLayout)终止应用程序。 但它只会因为constraintLayout.addView(小部件)不工作而终止应用

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

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

例如,
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" />

您的应用程序正在崩溃,因为您试图添加到
约束列表中的子视图未设置ID。要解决此问题,您可以使用要添加到布局中的
view.generateView()
为每个视图生成和ID,如下所示:

editTextNextGuest.setId(View.generateViewId());
editTextNextGuest.setId(R.id.edit_text_next_guest);
或者,您可以在
res/values
文件夹中创建
ids.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="edit_text_next_guest" type="id" />
</resources>
请注意,在将视图添加到布局之前,必须设置视图的ID