【发布时间】:2020-12-08 23:48:38
【问题描述】:
layout_bg.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<!--<stroke android:width="3dp" android:color="#B1BCBE" />-->
<corners android:radius="10dp"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
main_activity.xml
<ImageButton
android:background="@drawable/layout_bg"
android:src="@drawable/myicon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="@android:color/black"
android:minWidth="80.0dp"
android:minHeight="80.0dp"
android:padding="10px"
android:scaleType="fitCenter"
android:layout_alignParentBottom="true"
android:id="@+id/btnFoo"
android:layout_marginLeft="91.0dp"
/>
我正在将ImageButton 的背景设置为布局,这样我就可以拥有动态圆角,正如您在这张图片中看到的那样:
但是,我需要将该按钮的背景颜色更改为另一种纯色,例如红色。
如果我用btnFoo.SetBackgroundColor(Color.Red)设置背景颜色,那么圆角就没有了。
我假设是因为它将背景属性覆盖为纯色值而不是layout_bg.xml 文件。
如何设置此特定 ImageButton 的背景颜色,而不是我的 main_activity 中使用 layout_bg 作为背景的所有 ImageButtons?
【问题讨论】:
-
你需要用红色制作另一个drawable然后使用它:btnFoo.SetBackgroundResource(R.drawable.red_drawable);
-
@WowoOt 不理想,但有效。它比 MaterialButton 答案在下面给我的问题提供了更快的解决方案,因此如果您希望我接受,请发布答案。
-
@John 你不需要创建另一个drawable。只需申请
colorFilter。检查answer below。
标签: java android android-layout xamarin android-button