无法在 ImageView 上使用 setForeground 方法
Can't use setForeground method on ImageView
我想使用 setForeground 方法在我的 ImageView 中央显示一个 "play" 图标,以指示用户如果按下它就会播放视频。
目前我遇到无法解决的错误:
虽然文档说该方法应该可用,因为 API 1:
我正在使用构建工具版本 23.0.1 针对 API 23 进行编译。我的目标是最小 API 16.
即a documentation bug。 setForeground()
存在于 FrameLayout
来自 API 级别 1;从 API 级别 23 开始,它仅在 View
上。
您可以更改
minSdkVersion 16
和
minSdkVersion 23
或import android.support.annotation.RequiresApi;
到你的 class 和这个声明
@RequiresApi(api = Build.VERSION_CODES.M)
你在其中使用了 setForground
方法的 activity。
注意@RequiresApi(api = Build.VERSION_CODES.M)
中的M
代表API23
您可以使用下面的每一项代表特定的 API 而不是 M
BASE 1
BASE_1_1 2
CUPCAKE 3
DONUT 4
ECLAIR 5
ECLAIR_0_1 6
ECLAIR_MR1 7
FROYO 8
GINGERBREAD 9
GINGERBREAD_MR1 10
HONEYCOMB 11
HONEYCOMB_MR1 12
HONEYCOMB_MR2 13
ICE_CREAM_SANDWICH 14
ICE_CREAM_SANDWICH_MR1 15
JELLY_BEAN 16
JELLY_BEAN_MR1 17
JELLY_BEAN_MR2 18
KITKAT 19
KITKAT_WATCH 20
LOLLIPOP 21
LOLLIPOP_MR1 22
M 23
N 24
N_MR1 25
O 26
CUR_DEVELOPMENT 10000
由于在 API 级别 1 中为 FrameLayout
添加了 setForeground
方法,作为解决方法,您可以将视图包装在 FrameLayout
中,然后使用 setForeground
方法到布局,它将起作用,例如:
在你的xml中:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fl_item_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/niImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/imageView_description"
android:scaleType="fitCenter"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</FrameLayout>
然后在代码中,使用:
holder.flItemContainer.setForeground(ContextCompat.getDrawable(a, R.drawable.play));
我想使用 setForeground 方法在我的 ImageView 中央显示一个 "play" 图标,以指示用户如果按下它就会播放视频。
目前我遇到无法解决的错误:
虽然文档说该方法应该可用,因为 API 1:
我正在使用构建工具版本 23.0.1 针对 API 23 进行编译。我的目标是最小 API 16.
即a documentation bug。 setForeground()
存在于 FrameLayout
来自 API 级别 1;从 API 级别 23 开始,它仅在 View
上。
您可以更改
minSdkVersion 16
和
minSdkVersion 23
或import android.support.annotation.RequiresApi;
到你的 class 和这个声明
@RequiresApi(api = Build.VERSION_CODES.M)
你在其中使用了 setForground
方法的 activity。
注意@RequiresApi(api = Build.VERSION_CODES.M)
中的M
代表API23
您可以使用下面的每一项代表特定的 API 而不是 M
BASE 1
BASE_1_1 2
CUPCAKE 3
DONUT 4
ECLAIR 5
ECLAIR_0_1 6
ECLAIR_MR1 7
FROYO 8
GINGERBREAD 9
GINGERBREAD_MR1 10
HONEYCOMB 11
HONEYCOMB_MR1 12
HONEYCOMB_MR2 13 ICE_CREAM_SANDWICH 14
ICE_CREAM_SANDWICH_MR1 15
JELLY_BEAN 16
JELLY_BEAN_MR1 17
JELLY_BEAN_MR2 18
KITKAT 19
KITKAT_WATCH 20
LOLLIPOP 21 LOLLIPOP_MR1 22
M 23
N 24
N_MR1 25
O 26
CUR_DEVELOPMENT 10000
由于在 API 级别 1 中为 FrameLayout
添加了 setForeground
方法,作为解决方法,您可以将视图包装在 FrameLayout
中,然后使用 setForeground
方法到布局,它将起作用,例如:
在你的xml中:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fl_item_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/niImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/imageView_description"
android:scaleType="fitCenter"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</FrameLayout>
然后在代码中,使用:
holder.flItemContainer.setForeground(ContextCompat.getDrawable(a, R.drawable.play));