【问题标题】:Android ImageButton does not display image, even though it is visible in the Designer previewAndroid ImageButton 不显示图像,即使它在设计器预览中可见
【发布时间】:2017-03-18 23:34:32
【问题描述】:

我正在尝试在 Android Studio 中创建一个图像按钮。设计器预览正确显示图像,但是当我运行应用程序时,按钮中没有图像。只是一个尽可能小的空按钮。

我尝试过的事情:

  • 将图像从 Android drawable 文件夹复制到我自己的 drawable 文件夹中
  • 根据 stackoverflow 上的一些消息来源,由于存在错误而创建占位符图像会阻止文件夹中的第一张图像不显示
  • 使用不同的图片

这些都不适合我。就像整个功能被破坏了一样。有趣的是,当我将想要的图像设置为 imagebutton 的背景时,它可以正常工作。但它失去了它的“按钮外观”,所以这对我来说不是一个解决方案。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<org.osmdroid.views.MapView android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<LinearLayout
    android:id="@+id/search_ui"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:padding="1dp"
    android:background="@color/white"
    android:orientation="horizontal">
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/ic_menu_manage"
        android:id="@+id/imageButton"/>
    <EditText android:id="@+id/edit_search_string"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/search_string" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send"
        android:onClick="myClickHandler"/>
</LinearLayout>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:background="@color/white"
    android:layout_below="@id/search_ui">
    <TextView
        android:id="@+id/display"
        android:textSize="16sp"
        android:textStyle="bold"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="New Text" />
</LinearLayout>

【问题讨论】:

    标签: java android xml android-studio


    【解决方案1】:

    尝试只使用 src 而不是 srcCompat 看看它是否有效

    【讨论】:

    • 是的,出于某种原因,我需要等待几分钟。
    • 它对我有用。你能解释一下srcsrcCompat有什么区别吗?
    • @kuzdu,请通过此链接stackoverflow.com/questions/40624554/… 如果您仍有理解问题,请告诉我
    【解决方案2】:

    我的问题是图片的名称很长,而且里面有一个连字符。我简化了图纸的文件名,它开始工作了。令人困惑的一点是图像(修复之前)显示在资源查看器中,但没有显示在图像按钮中。按钮缩小到什么都没有,好像图像非常小。

    【讨论】:

      【解决方案3】:

      从 srcCompat 中删除你的图片

      将此图像设置为 src。如果找不到 src 则搜索它并在 src

      中输入您的图像路径

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-25
        • 2018-11-20
        • 2016-12-13
        相关资源
        最近更新 更多