2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

LinearLayoutでdividerを表示する

Posted at

LinearLayoutには要素間にDividerを表示する機能があります。

使い方

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorSurfaceContainerLow"
    android:divider="@drawable/divider"
    android:dividerPadding="16dp"
    android:paddingHorizontal="16dp"
    android:showDividers="middle|beginning|end"
    >
    ...
</LinearLayout>

この指定で、TextViewを間に3つ入れた場合、以下のような表示になります。

android:divider
dividerとして表示するdrawableを指定します。
Verticalの場合の高さ、Horizontalの場合の幅はdrawableのIntrinsicWidth/IntrinsicHeightが使われるため、GradientDrawableでsizeを指定していないなど、サイズを持たないDrawableを指定した場合は指定しても描画されません。

android:showDividers
デフォルト値はnoneであるため指定しないと描画されません。
middle / begginning / end の論理和で指定します。名前の通り、要素の間、最初の要素の前、最後の要素の後それぞれにDividerを描画するかの指定です。

android:dividerPadding
Horizontalの場合の上下、Verticalの場合の左右に指定のpadding分のdividerを描画しないように指定できます。非対称なpaddingは指定できません。


LinearLayoutというと、超基本的なレイアウトで、昔からずっと使っていましたが、最近までこんなことができるってことを知りませんでした。
いままでdividerをViewで作って間に挟んでたよ…って人は、私一人では無いはず…

以上です。

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?