Button button = findViewById(R.id.button);もしくは、追加したい適当なオブジェクトを作成し、ConstraintLayoutにViewを追加する.
Button button = new Button(this); constrintLayout.addView(button)
※先にaddViewしないと、次のlayoutParamsにnullが入る(エラーが発生する)ので注意.
次に、上のViewのLayoutParamsを取得し、適当に設定する. 親View(?)と連結したいときは、PARENT_IDを代入する. ほかのViewと連結したいときは、View#getIdメソッドを使う. LayoutParamsのフィールドについては、xmlのものとほとんど同じなので、そちら(例えば、Android Studioのデザインビューとか)を見るとわかりやすいと思う.
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams)button.getLayoutParams(); layoutParams.startToEnd = anotherView.getId(); layoutParams.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID; layoutParams.topToTop = ConstraintLayout.LayoutParams.PARENT_ID; layoutParams.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID; layoutParams.verticalBias = (float) 0.1; layoutParams.horizontalBias = (float) 0.1; button1.setLayoutParams(layoutParams);最後に、上の変更を適用する.
button.setLayoutParams(layoutParams);他にもこちらと同様に、ViewのWidthやHeight、Marginを変更できたりする(MarginLinearLayoutにキャストしなくてもよい、次の適用例を参照).
適用例
ConstraintLayout上に2つのボタンを表示させる.activity_main.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/constraintLayoutParamsLayout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.blogspot.ko-ika.constraintlayoutparams.MainActivity"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button2" /> </android.support.constraint.ConstraintLayout>
activity_main.xml
package jp.blogspot.ko-ika.constraintlayoutparams; import android.support.constraint.ConstraintLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Button; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button1 = findViewById(R.id.button1); Button button2 = findViewById(R.id.button2); ConstraintLayout constraintLayout = findViewById(R.id.constraintLayoutParamsLayout); // Button1の設定 ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams)button1.getLayoutParams(); layoutParams.topMargin = 8; layoutParams.rightMargin = 16; layoutParams.startToStart = ConstraintLayout.LayoutParams.PARENT_ID; layoutParams.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID; layoutParams.topToTop = ConstraintLayout.LayoutParams.PARENT_ID; layoutParams.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID; layoutParams.verticalBias = (float) 0.1; layoutParams.horizontalBias = (float) 0.1; button1.setLayoutParams(layoutParams); // Button2の設定 layoutParams = (ConstraintLayout.LayoutParams)button2.getLayoutParams(); layoutParams.leftMargin = 16; layoutParams.startToEnd = button1.getId(); layoutParams.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID; layoutParams.topToTop = ConstraintLayout.LayoutParams.PARENT_ID; layoutParams.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID; layoutParams.verticalBias = (float)0.1; layoutParams.horizontalBias = 0; button2.setLayoutParams(layoutParams); } }
実行例
さらに
ConstraintSetを使うと同じ様に設定できるらしい. が、この方法だと、Biasが設定できない? また、Viewが実際に配置される(?)まで、そのIDが取得できないみたいなので、注意が必要. 実際に、上の適用例を次のようにしてみる.activity_main.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/constraintLayoutParamsLayout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.blogspot.ko-ika.constraintlayoutparams.MainActivity"> </android.support.constraint.ConstraintLayout>
activity_main.xml
Button button1 = new Button(this); Button button2 = new Button(this); ConstraintLayout constraintLayout = findViewById(R.id.constraintLayoutParamsLayout); // Button1の設定 constraintLayout.addView(button1); button1.setText("Button1"); ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams)button1.getLayoutParams(); layoutParams.topMargin = 8; layoutParams.rightMargin = 16; layoutParams.startToStart = ConstraintLayout.LayoutParams.PARENT_ID; layoutParams.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID; layoutParams.topToTop = ConstraintLayout.LayoutParams.PARENT_ID; layoutParams.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID; layoutParams.verticalBias = (float) 0.1; layoutParams.horizontalBias = (float) 0.1; button1.setLayoutParams(layoutParams); // Button2の設定 constraintLayout.addView(button2); button2.setText("Button2"); layoutParams = (ConstraintLayout.LayoutParams)button2.getLayoutParams(); layoutParams.leftMargin = 16; layoutParams.startToEnd = button1.getId(); layoutParams.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID; layoutParams.topToTop = ConstraintLayout.LayoutParams.PARENT_ID; layoutParams.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID; layoutParams.verticalBias = (float)0.1; layoutParams.horizontalBias = 0; button2.setLayoutParams(layoutParams);変更箇所をマークアップした. すると、次のようになってしまう.
0 件のコメント:
コメントを投稿