【问题标题】:Drawable not showing up可绘制不显示
【发布时间】:2011-11-20 10:41:29
【问题描述】:

我有一个相当简单的 xml 文件,其中有一个图像按钮。图像在图形布局 xml 设计器上显示良好,在我运行开发构建时显示良好,但是一旦我创建签名的 apk 文件并运行它,图像就不再显示。它只是一个空按钮。我想不出原因,有什么想法吗? xml 文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/navigation_root"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/navigation_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="TextView"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <SeekBar
        android:id="@+id/navigation_seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:padding="5dp" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp" >

        <ImageButton
            android:id="@+id/part_select_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/chapter_select" />

        <Button
            android:id="@+id/navigation_ok_button"
            android:layout_width="75dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/ok" />

        <Button
            android:id="@+id/navigation_cancel_button"
            android:layout_width="75dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/cancel" />
    </LinearLayout>
</LinearLayout>

图像@drawable/chapter_select 是一个相当小的 (41*41) png 文件,位于 res/drawable 文件夹中。

【问题讨论】:

    标签: android image button


    【解决方案1】:

    这似乎是 android 的一个错误,有时可绘制文件夹中的第一张图像不会显示。在可绘制文件夹中添加了一个名为 aaaa.png 的虚拟图像,问题得到解决。在这里找到答案:ImageButton does not display a particular drawable

    【讨论】:

      【解决方案2】:

      其中一个原因是:

      如果您在 layout.xml 中使用Vector 文件作为drawableLeftdrawableRight(或drawableStartdrawableEnd),那么您必须使用androidx.appcompat.widget.AppCompatButton (以前是 android.support.v7.widget.AppCompatButton)而不是 Button

      在我的情况下,ButtonTextview 等简单视图不支持将 Vector 文件作为 drawableLeftdrawableRight(或 drawableStartdrawableEnd)。

      【讨论】:

      • 虽然 OP 的案例是 ImageView 并且这个答案可能不适用于它,但这个答案对我的案例很有帮助。因为我会在 layout.xml 中将 drawableStart 设置为 Button。
      【解决方案3】:

      遇到了同样的问题并通过删除所有特殊字符来解决它。在我的情况下,它是文件名中的破折号“-”:

      background-720.png =&gt; background.png

      【讨论】:

        【解决方案4】:

        尝试将图像放入drawable-hdpi和drawable-mdpi文件夹 取决于您运行应用程序的设备,在这些文件夹中搜索图像...

        但是放入drawable意味着图像应该在任何地方都可用,但有时(取决于你的清单设置)这不可能是真的,我的意思是你可以打开兼容模式。

        您也可以在运行时尝试动态地将图像设置为视图

        iv.setImageResource(R.drawable.somethig);
        

        【讨论】:

        • 我不想把它放在特定的屏幕密度文件夹中,因为我希望所有设备都能够在不复制的情况下使用图像。动态设置图像并没有改变任何东西,它仍然没有显示在签名的版本上。感谢您尝试 Luk。
        【解决方案5】:

        我的情况很奇怪。在将 FireBase 崩溃报告集成到我的应用程序之前,一切都是正确的。

        我刚刚添加了 compile 'com.google.firebase:firebase-crash:11.0.1' & DrawableLeft vanished 。当我浏览 xml 时,注意到一个警告(如下所示)。

        所以添加了android:drawableStart & 问题消失了。

        我仍然想知道 FireBase 崩溃报告与相同的关系。

        使用左/右而不是开始/结束属性使用重力#LEFT 和 Gravity#RIGHT 可能会在布局渲染时导致问题 文本从右向左流动的语言环境。使用重力#START 和 重力#END 代替。

        同样,在 XML 重力和 layout_gravity 属性中,使用 start 而不是离开。对于 XML 属性,例如 paddingLeft 和 layout_marginLeft,使用 paddingStart 和 layout_marginStart。

        注意:如果您的 minSdkVersion 小于 17,您应该同时添加 旧的左/右属性以及新的开始/右属性。 在较旧的平台上,不支持 RTL 并且开始/正确 属性是未知的,因此被忽略,你需要旧的 左/右属性。

        有一个单独的 lint 检查可以捕获该类型的错误。 (注意:对于 Gravity#LEFT 和 Gravity#START,您可以使用这些常量 即使针对较旧的平台,因为起始位掩码是 左位掩码的超集。因此,您可以使用重力=“开始” 而不是gravity="left|start"。)

        【讨论】:

          【解决方案6】:

          检查您的图片尺寸。如果您在实际部署时使用了不必要的大资产,尽管在设计器中看起来正确,但它可能不会显示。

          【讨论】:

            【解决方案7】:

            好吧!在我的情况下,设置 MinifyEnabled falseshrinkResources false 现在工作正常。

            我正在从 drawable 中获取图像。它在调试版本中工作得非常好,但是在发布版本的 apk 之后它有时会显示空白的 ImageView。

            minifyEnabled false shrinkResources true

            **

            查看截图

            **

            希望这可以帮助任何人。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-06-19
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多