【问题标题】:Buttons background turns to blue color (not same as original)按钮背景变为蓝色(与原版不同)
【发布时间】:2021-06-29 17:15:52
【问题描述】:
我的问题似乎很简单,但我不知道如何解决。
我有几个按钮,它们都有一个背景图像。问题是,所有图像都变成蓝色
看起来像这样:
原图:
这是我的 xml 代码的一部分:
<Button
android:id="@+id/memory_button1"
android:layout_weight="1"
android:maxWidth="30dp"
android:maxHeight="30dp"
android:onClick="@{()->viewModel.showTile(1)}"
android:background="@{viewModel.button001}">
</Button>
图标网址:
https://www.flaticon.com/packs/restaurant-225
【问题讨论】:
标签:
android
android-studio
android-layout
android-xml
【解决方案1】:
使用ImageView 作为具有波纹点击效果的按钮的更好方法是
<ImageView
android:id="@+id/memory_button1"
android:layout_weight="1"
android:maxWidth="30dp"
android:maxHeight="30dp"
android:background="?selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:onClick="@{()->viewModel.showTile(1)}"
android:src="@{viewModel.button001}"/>
您甚至可以将其包装在 CardView
【解决方案2】:
如果您想在按钮中拥有唯一的图像。您可以将其替换为ImageView,这样您就可以完全控制图像的绘制方式。
<ImageView
android:id="@+id/memory_button1"
android:layout_weight="1"
android:maxWidth="30dp"
android:maxHeight="30dp"
android:onClick="@{()->viewModel.showTile(1)}"
android:src="@{viewModel.button001}">
</ImageView>
您将为ImageView 选择缩放类型,这样您就可以在不拉伸的情况下渲染图像。