Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.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
Android 使用ConstraintSet以编程方式将按钮添加到CONSTRAINTLAYOUT_Android_Android Button_Android Constraintlayout_Constraintset - Fatal编程技术网

Android 使用ConstraintSet以编程方式将按钮添加到CONSTRAINTLAYOUT

Android 使用ConstraintSet以编程方式将按钮添加到CONSTRAINTLAYOUT,android,android-button,android-constraintlayout,constraintset,Android,Android Button,Android Constraintlayout,Constraintset,大家好 我试图以编程方式将按钮添加到ConstraintLayout并将ConstraintSet设置为此类按钮。 当我试图从布局中关闭constraintSet时,问题出现了,因为我添加的按钮没有ID Button button = new Button(this); ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(250, ConstraintLayout.LayoutParams.WRAP_

大家好

我试图以编程方式将按钮添加到
ConstraintLayout
并将
ConstraintSet
设置为此类按钮。 当我试图从布局中关闭constraintSet时,问题出现了,因为我添加的按钮没有ID

Button button = new Button(this);

ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(250, ConstraintLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(15,15,15,15);

button.setLayoutParams(params);
button.setText(returnObject.getFunction());
button.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
button.setTextColor(R.drawable.custom_button_color);
button.setBackgroundResource(R.drawable.custom_button);

ConstraintLayout container = findViewById(R.id.highLevelContainer);
container.addView(button);

ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(container); // Here I get the error
constraintSet.connect(button.getId(), ConstraintSet.END , container.getId(), ConstraintSet.END,0);
constraintSet.connect(button.getId(), ConstraintSet.START, container.getId(),ConstraintSet.START,0);
constraintSet.connect(button.getId(), ConstraintSet.TOP, container.getId(), ConstraintSet.TOP);
constraintSet.applyTo(container);
这是控制台上生成的错误:

java.lang.RuntimeException: All children of ConstraintLayout must have ids to use ConstraintSet
    at android.support.constraint.ConstraintSet.clone
我希望避免将一个随机数作为ID分配给按钮,因为它是在循环中创建的。
你知道如何解决这个问题吗?

如果你想使用
setId(View.generateViewId())
避免一个随机数,例如,你希望为你的子视图设置什么id?太好了。非常感谢。例如,如果您想使用
setId(View.generateViewId())
避免使用随机数,您希望为您的子视图设置什么id?太好了。谢谢。