LoginSignup
5
3

More than 1 year has passed since last update.

android:layout_width="0dp"て何?

Last updated at Posted at 2022-03-26

この記事は

Androidで画面を構成していく時、heightwidthなどを指定することは頻繁にあることと思います。

このような時に、多くの場合は、wrap_contentmatch_parentを使いますが、たまにandroid:layout_width="0dp"のような表現を見ることがあります。
ではこれらの違いを見ていきましょう

wrap_content:子に合わせる

これはその中に入るビューの大きさに任せるよ、という意味です。
つまり、例えばlayout_height="wrap_content"で指定されたButtonの中にlayout_height="100dp"のTextViewを入れたとします。その場合、Buttonの高さはTextViewに合わせて100dpになります。

match_parent:親に合わせる

これは、親(1つ上のビュー)の大きさに合わせるよ、という意味です。
例えば、layout_height="100dp"としているButtonの中にlayout_height="match_paretnt"で高さを指定したTextViewを入れるとします。その場合、TextViewの高さは親であるButtonと同じ高さである100dpに設定されます。

0dp:match_constraint

:cat: これはConstraintLayoutを使う上での話です!
ここでタイトルに戻ります。android:layout_width="0dp"とは何なのでしょうか。
この0dpとは実はmatch_constraintのことを言っています。
つまり、親がConstraintLayoutの場合、相対的に変化する親のConstraintLayoutの高さに応じて中身も高さを変えるよ、ということになります。

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3