はじめに
今回はレビューで ConstraintLayoutの中で使われているLayoutに
android:layout_width="wrap_content"
android:layout_height="wrap_content"
を見つけ、layout_width="0dp"にできそうならしたほうがいいかもです と意見した際に
なぜlayout_width="0dp"にした方がいいのかを自分の中で疑問に思ったので調べました。
その内容を自分の備忘録のために記事に残します。
レイアウトの大きさを数字以外で指定する
android:layout_widthやandroid:layout_heightには数字以外にも
match_parent は 親に合わせる(なるべく最大サイズ)
wrap_content は子に合わせる(なるべく最小サイズ)
match_constraint は親がConstraintLayoutの場合に 変化する親のサイズに合わせてサイズを変更される
などで指定することができます。
本題
では、なぜlayout_width="0dp"にしなければいけないのでしょうか。
0dpを指定するとmatch_constraintを指定した時と同じになります。
また、layout_weightを合わせて指定する際に 「layout_weightでの幅計算」と「wrap_contentの幅計算」が両方計算するため無駄になってしまう。
パフォーマンスを上げるためにも、layout_weightを使う際にはLayoutサイズを0dpにした方が良いでしょう。