简体   繁体   English

为什么我的 Android Drawable 上面显示了一种颜色?

[英]Why is my Android Drawable showing up with a color on top of it?

I am developing a simple, daily task reporting Android Studio app as an internal tool for the IT department I work for.我正在开发一个简单的日常任务报告 Android Studio 应用程序作为我工作的 IT 部门的内部工具。 I'm trying to make it as informative and easy to use as possible, adding user friendly UI.我正在努力使它尽可能提供信息和易于使用,添加用户友好的 UI。 For this, I'm using AlertDialog to interact with the user, such as showing information, warnings and errors.为此,我使用 AlertDialog 与用户交互,例如显示信息、警告和错误。 The problem comes when I try to set an icon to the AlertDialog.当我尝试为 AlertDialog 设置一个图标时,问题就来了。 It technically works but it shows a white icon, like this: Screenshot它在技术上是可行的,但它显示一个白色图标,如下所示:屏幕截图

When it is supposed to look like this: Icon当它应该看起来像这样时:图标

I believe Android 12 expects me to use a drawable vector, because when I use the ic_launcher_foreground drawable file it displays just fine.我相信 Android 12 希望我使用可绘制矢量,因为当我使用ic_launcher_foreground可绘制文件时,它显示得很好。 I just wanted to know if there's any way to override that so I can display a plain image file instead of a drawable vector.我只是想知道是否有任何方法可以覆盖它,以便我可以显示纯图像文件而不是可绘制矢量。 (I was wrong asuming this was the problem.) I have tried to find documentation or any posts/questions about this specific problem but I think I've been searching the wrong places. (我认为这是问题所在是错误的。)我试图找到有关此特定问题的文档或任何帖子/问题,但我认为我一直在寻找错误的地方。

Here's the code that builds and shows my AlertDialog (Java):这是构建和显示我的 AlertDialog (Java) 的代码:

private void showGpsDialog(){
    final String dialog_title = getResources().getString(R.string.dialog_important_title);
    final String dialog_message = getResources().getString(R.string.gps_disabled_dialog_message);
    final String dialog_positive = getResources().getString(R.string.dialog_understood);
    AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AlertDialogRegular);
    builder.setMessage(dialog_message)
            .setPositiveButton(dialog_positive, (dialogInterface, i) -> {
            })
            .setTitle(dialog_title)
            .setIcon(R.drawable.ic_info_drawable);
    AlertDialog dialog = builder.create();
    dialog.show();
}

Here's the drawable icon xml:这是可绘制图标 xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:opacity="opaque">
    <item
        android:width="15dp"
        android:height="15dp"
        android:gravity="center"
        >
        <bitmap android:src="@drawable/info_icon"/>
    </item>
</layer-list>

Turns out I was making a huge, probably stupid but silent mistake.事实证明我犯了一个巨大的,可能是愚蠢但悄无声息的错误。 I was using a custom theme in the themes.xml file, and I had this line <item name="tint">@color/white</item> which was making all my drawables have white on top of the image it was supposed to be there, but I noticed only when I tried to display an ImageView and the same problem as my AlertDialog showed up.我在themes.xml文件中使用了一个自定义主题,并且我有这一行<item name="tint">@color/white</item>这使得我所有的可绘制对象在图像之上都应该是白色的在那里,但我只有在尝试显示 ImageView 时才注意到,并且出现了与 AlertDialog 相同的问题。 I just removed that line because I found out that I did not need it, and that solved the problem.我刚刚删除了那条线,因为我发现我不需要它,这解决了问题。

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

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