简体   繁体   English

Glide 加载的 Drawable 不能设置为 Button 的背景,适用于 ImageButton

[英]Glide-loaded Drawable cannot be set as background for Button, works for ImageButton

I am unable to load a Drawable from a bitmap, into a Button instance:我无法将位图中的 Drawable 加载到 Button 实例中:

        Glide.with(hostFragment.getActivity())
                .load(internetHttpUrl)
                .apply(new RequestOptions()
                        .override(150, 150))
                .into(new CustomTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        btnItem.setBackground(resource);
                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {
                    }
                });

For all image/jpg I tried, nothing is displayed.对于我尝试过的所有图像/jpg,没有显示任何内容。 Same goes for one PNG image.一张PNG图像也是如此。 Another PNG image with transparent background does display, transparency is taken into account but the color palette seems completely wrong.另一个具有透明背景的 PNG 图像确实显示,考虑了透明度,但调色板似乎完全错误。

The same code works fine, regardless of the image being used, for ImageButton instances.对于 ImageButton 实例,无论使用何种图像,相同的代码都可以正常工作。

Is there some specific code to write for Buttons?是否有一些特定的代码可以为按钮编写?

My XML looks like this:我的 XML 如下所示:

<Button
    android:id="@+id/btnItem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:textSize="10sp"
    android:text="DUMMY"/>

When using an ImageButton:使用 ImageButton 时:

<ImageButton
    android:id="@+id/btnItem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1" />

When inspecting the bitmap with the debugger ('View bitmap' functionality), Android Studio reveals the bitmap is valid under all circumstances.当使用调试器(“查看位图”功能)检查位图时,Android Studio 显示位图在所有情况下都是有效的。

As @commonsware says here , recent Android Studio versions set the default theme in new projects as Theme.MaterialComponents.DayNight.DarkActionBar .正如@commonsware所说,最近的 Android Studio 版本将新项目中的默认主题设置为Theme.MaterialComponents.DayNight.DarkActionBar So, a side effect of this is that any Button get turned into MaterialButton , and these buttons ignore the android:background attribute.因此,这样做的副作用是任何Button都会变成 M MaterialButton ,并且这些按钮会忽略android:background属性。

Then, I recommend you to use androidx.appcompat.widget.AppCompatButton instead because you want to use a default Button to change that attribute.然后,我建议您改用androidx.appcompat.widget.AppCompatButton因为您想使用默认按钮来更改该属性。 As a side note, if the previous theme didn't set, this is the project's default button when we use Button .附带说明,如果之前的主题没有设置,这是我们使用Button项目的默认按钮

Happy coding!快乐编码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM