ConstraintLayout |
Constraint(制約)はどんな意味があるのだろうか? Hello world.のactivity_main.xmlwoを[Design]のタブでみると、 なにやら怪しい(?)波線が表示されれいる。[Code]のタブで見ると、 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> app:layout_constraintBottom_toBottomOf="parent" を削除。下の波線と上の波線が消えた。下の丸の塗りつぶしがなくなった。 app:layout_constraintEnd_toEndOf="parent" を削除。左右の波線が消えた。右の丸の塗りつぶしがなくなった。 app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" を削除。右と上の丸の塗りつぶしがなくなった。 しかし、[Code]のタブで、 エラーか警告かを知らせる表示になっている。 Runできるか? できた。つまりこれは警告のようです。[Design]のタブでみると, Constraintがないと文句を言っている。波線の正体は、 app:layout_constraintXXXX に関係している。つまり、上下左右がペアレントに関係する。 画面のセンター配置される。 Hello world.ではわかりにくいのでボタンを複数設置した事例 で考えてみます。 Hello world.のテキストビューを削除します。 ボタンを設置します。 ConstraintLayoutでは任意の場所に設置できます。 次に左の丸をドラッグして左の隅までドラッグします。 左の隅でマウスのボタンを離すと、 ボタンが左隅に接続します。 同様に上の丸を操作します。 ボタンが上隅に接続します。 これでConstraintsの警告表示がなくなりました。コードは、 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" が書かれています。 このボタンを任意の位置にドラッグするとどうなるか? 移動できました。矢印が表示され200と156の数字が表示されています。 この数値の単位はdpです。dpって何だ? ピクセル密度(dpi)が160のとき、 1dp = 1px です。160以外のときは、 px = dp * (該当スマフォのdpi / 160) です。ピクセル密度が違っても同じ位置、同じ大きさになるように した単位です。実際には、 同じに見えてもピクセルサイズが違います。 ConstraintLayoutの利点:好きな位置にはりつけられる。 制約をされた事例 bUTTON2はBUTTON5に制約を受けています。BUTTON5は大きさをかえたり 位置を変えたりできます。しかし、BUTTON2は、 横幅の変更は不可。縦幅の変更は可。 ドラッグは不可。 上記の制約があります。 制約の付け方。 app:layout_constraint[自分の辺]_to[相手の辺]Of="@+id/相手のid" app:layout_constraint[自分の辺]_to[相手の辺]Of="parent" [ ]の部分には下の画像の言葉が入ります。 上記ボタンの制約のソースです。 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="156dp" android:layout_marginTop="88dp" android:text="Button5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="4dp" android:text="Button2" app:layout_constraintEnd_toEndOf="@+id/button5" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="@+id/button5" app:layout_constraintTop_toBottomOf="@+id/button5" /> </androidx.constraintlayout.widget.ConstraintLayout> この制約ってどんな場面で有効なのだろう? デザインがきっちり出来上がって、それを少し移動させたいとき 制約があれば全部そろって移動できる。くらいしか思いつかない です。 制約よりも自由に配置できることが最大のメリットではないかと 思います。 |