Viewにタッチフィードバック持たせるときどうしていますか?
何もカスタマイズしないならこんな感じで android:background
属性に ?attr/selectableItemBackground
設定することが多いと思います。
<View
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground" />
簡単ですね。
では次に、子の View を持つ ViewGroup に対して子に重ねてタッチフィードバックを表示したいときを考えます。(android:background
だと子の裏に表示される)
android:foreground
を使ってこんな感じに。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/background_light"
android:foreground="?attr/selectableItemBackground"
android:orientation="horizontal">
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="16dp"
android:text="Title" />
</LinearLayout>
Nougat 端末で確認して正常に動く!と思ったら、Lollipop で動かない。(タッチフィードバックが表示されない)
ドキュメント見ると API Level1 から存在するのに。。setForegroundだけど
View の android:foreground は AndroidM から動く
ググるとこんな Issue が。
View.setForeground wrong API level
要は AndroidM で追加されたのにドキュメントでは API Level1 からと誤った情報がのっているぽい。
ただし、 android:foreground は FrameLayout では動く
View.setForeground
は AndroidM からだけど、FrameLayout だけは API Level1 から使える。
android 2.0_r1 の FrameLayout に setForeground の存在を確認。(ちなみにAPI level25では消えてた。多分Viewに追加されたから 23でも多分消えてると思う)
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.0_r1/android/widget/FrameLayout.java#FrameLayout.setForeground%28android.graphics.drawable.Drawable%29
もちろん CardView 等も FrameLayout を継承してるから下位端末で動く。