LoginSignup
0

More than 1 year has passed since last update.

[Android] [Kotlin] RecyclerViewやListViewで上部や下部の余白を均等に表示したい!

Posted at

できたこと

RecyclerViewやListViewでいい感じにPadding、Marginしたいけど、上手くできない方向けです。
例えば、RecyclerViewのItemをmargin="10dp"とした場合、
上部と下部は margin="10dp" できるんだけど、
間にあるItemは margin="10dp" + margin="10dp" で間隔が異なる。
それならbottomやendだけにmarginをつければいいや。ってなるかもしれませんが、
私は "全部均等に余白を作りたい" 派の人間ですので、
その方法を見つけました!

レイアウト

activtiy_main.xml
<RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="10dp"
    // コレ
    android:clipToPadding="false"/>
recyclerview_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_marginTop="10dp">

    ~
    ~
    ~

</LinearLayout>

clipToPaddingは、Viewのpaddingが0でない場合、その子のViewに対して、paddingを適用"しない"かどうかを切り替えるものです。
適用しないかどうかなので、Trueで適用しない、Falseで適用するとなります。デフォルト値はTrueとなっております。

なので、paddingを指定した上で、clipToPaddingにfalseを指定すれば、RecyclerViewとその要素の間に余白を空けることができます!
是非試してみて下さい。

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
0