###動的にjavaコードで追加したボタンに、constraintは適応できないのでしょうか?
題目通りです。
まず静的な、つまりxmlで作ったボタン1の下に動的な、つまりコードで作ったボタン2を追加します。
ここまではうまくいきました。
しかし、その動的に作ったボタン2の下に、さらに動的に新たなボタン3をつけようとしたのですが、制約がうまくいってないらしく、新たなボタン3は左上に出てしまいます。
これは動的にjavaコードで追加したボタンに、constraintは適応できない、ということなのでしょうか?それとも私がなにか間違っているのでしょうか?
以下にコードを記します。
どうかご教授いただけたら幸いです・・・!
lang
1 //constraintlayoutのインスタンス取得。 2 ConstraintLayout constraintLayout=content.findViewById(R.id.select_box); 3 //一章のbuttonのIDを取得。 4 Button selectone=(Button)content.findViewById(R.id.stage_1); 5 //二章のボタンを作る。 6 Button selecttwo =new Button(getContext()); 7 constraintLayout.addView(selecttwo); 8 9 selecttwo.setText(R.string.stage_two); 10 selecttwo.setId(2); 11 //一章のボタンの下に制約する。 12 ConstraintLayout.LayoutParams layoutParams_two=(ConstraintLayout.LayoutParams)selecttwo.getLayoutParams(); 13 layoutParams_two.startToStart=selectone.getId(); 14 layoutParams_two.topToBottom=selectone.getId(); 15 layoutParams_two.topMargin=32; 16 selecttwo.setLayoutParams(layoutParams_two); 17 //三章のボタンを作る。 18 Button selectthree=new Button(getContext()); 19 constraintLayout.addView(selectthree); 20 21 selectthree.setText(R.string.stage_three); 22 selectthree.setId(3); 23 //二章のボタンの下に制約する。 24 ConstraintLayout.LayoutParams layoutParams_three=(ConstraintLayout.LayoutParams)selectthree.getLayoutParams(); 25 layoutParams_three.startToStart=selecttwo.getId();//二章のボタンの取得ができていない・・・? 26 layoutParams_three.topToBottom=selecttwo.getId(); 27 layoutParams_three.topMargin=32; 28 selectthree.setLayoutParams(layoutParams_three); 29 30
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。