LoginSignup
3
4

More than 5 years have passed since last update.

RecyclerViewにElevationを設定して影をつける方法

Last updated at Posted at 2017-06-12

RecyclerViewに、android:elevation を設定するだけでは影は表示されません。親のViewGroupに、android:clipToPadding="false" を追加すると影が表示されます。
RecyclerViewのandroid:backgroundは透過でない必要があります。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:clipToPadding="false"
    android:gravity="center"
    android:padding="32dp">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/list"
        android:name="com.example.elevation.ItemFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_blue_light"
        android:elevation="2dp"
        app:layoutManager="LinearLayoutManager"
        tools:context="com.example.elevation.ItemFragment"
        tools:listitem="@layout/fragment_item" />

</RelativeLayout>

単純にCardViewで囲ってやっても良いかもしれません。

<android.support.v7.widget.CardView
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     app:cardCornerRadius="4dp"
     app:cardUseCompatPadding="true">

     <android.support.v7.widget.RecyclerView
        android:id="@+id/list"
        android:name="com.example.elevation.ItemFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="LinearLayoutManager"
        tools:context="com.example.elevation.ItemFragment"
        tools:listitem="@layout/fragment_item" />

</android.support.v7.widget.CardView>

(参考)

3
4
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
3
4