LoginSignup
5
2

More than 5 years have passed since last update.

Lollipopより前の端末でCardViewに余白が入らないようにする

Last updated at Posted at 2017-10-22

CardViewを作成する際、Lollipopより前の端末だと、カード周りに余白が入ってしまいます。
もともと余白のあるデザインであればそんなに気になりませんが、ImageViewをカードいっぱいに持つデザインの場合には調整が必要です。

Lollipop以降 Lollipopより前

対応

SupportLibraryのバージョンが24.2.0以降であれば、CardViewへ

app:cardPreventCornerOverlap="false"

を追加するとLollipopより前の端末でも余白がなくなります。

公式によると、「Lollipopより前の端末ではカードの角丸とカード上のViewの境界が重なるのを避けるために、勝手にPaddingを追加するよ」とあり、falseにするとPaddingを追加しなくなります。

On pre-Lollipop platforms, CardView does not clip the bounds of the Card for the rounded corners. Instead, it adds padding to content so that it won't overlap with the rounded corners. You can disable this behavior by setting this field to false.

注意:これでPaddingはなくなるのですが、ImageViewが角丸になるわけではないので、RoundedImageViewライブラリを使うなどして角丸にしてあげる必要があります。

補足

上記オプションは、Lollipop以降の端末には基本的に影響がありません。app:cardUseCompatPadding="true"を指定して、Lollipop前の挙動に設定している場合には、影響を受けます。

Setting this value on Lollipop and above does not have any effect unless you have enabled compatibility padding.

参考

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