[英]Setting drawable to floating action button ImageView in android
ImageView icon = new ImageView(this);
icon.setImageDrawable(R.drawable.alarm);
When I want to set drawable icon it gives me error that android graphics drawable can not apply to ImageView
?当我想设置 drawable 图标时,它给了我一个错误,即 android graphics drawable 不能应用于ImageView
?
The problem is you are passing ID of your resource in问题是您正在传递资源的 ID
icon.setImageDrawable(R.drawable.alarm);
method but this method takes only Drawable
as parameter.方法,但此方法仅将Drawable
作为参数。 You should change your above call to this你应该把你上面的电话改成这个
icon.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.alarm));
Or you should call this method instead.或者你应该调用这个方法。
icon.setImageResource(R.drawable.alarm);
请改用setImageResource(R.drawable.alarm)
方法。
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/addPostBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="58dp"
android:clickable="true"
android:focusable="true"
android:padding="0dp"
android:scaleType="center"
app:borderWidth="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/mainBottomNavBar"
app:maxImageSize="45dp"
android:backgroundTint="@color/White"
app:srcCompat="@drawable/add_icon"
app:useCompatPadding="true" />
Use the srcCompat keyword.使用 srcCompat 关键字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.