Android: Drawable not showing up

2019-03-19 23:50发布

I have a fairly simple xml file that has an image button in it. The image shows up fine on the Graphical Layout xml designer, shows up fine when I run a development build, but as soon as I create the signed apk file and run it, the image no longer shows up. It's just an empty button. I can't think of a reason why, any ideas? The xml file looks like this:

<?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" >
</TextView>

<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" >
</SeekBar>

<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" >
    </ImageButton>

    <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>

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

The image @drawable/chapter_select is a fairly small (41*41) png file that is in the res/drawable folder.

5条回答
何必那么认真
2楼-- · 2019-03-20 00:00

Had the same issue and resolved it by removing all special characters. In my case it was dashes '-' in the filename:

background-720.png => background.png.

查看更多
戒情不戒烟
3楼-- · 2019-03-20 00:02

try to put the image in drawable-hdpi and drawable-mdpi folder depends on what device you run you app , the image is searched in these folders...

But puting in drawable means that the image should be available everywhere, but somethimes (depends on your manifest settings) this could not be true, I mean you can turn of the compatibility mode.

also you can try dinamically at run time to set the image to the view

iv.setImageResource(R.drawable.somethig);
查看更多
叼着烟拽天下
4楼-- · 2019-03-20 00:02

My situation was weird.Everything was correct until integrating FireBase Crash report to my Application.

I just added compile 'com.google.firebase:firebase-crash:11.0.1' & DrawableLeft vanished .When i went through the xml , noticed a warning (In lined below).

So added android:drawableStart & issue gone.

Still I am wondering about the relation of FireBase Crash reporting to the same.

Using left/right instead of start/end attributes Using Gravity#LEFT and Gravity#RIGHT can lead to problems when a layout is rendered in locales where text flows from right to left. Use Gravity#START and Gravity#END instead.

Similarly, in XML gravity and layout_gravity attributes, use start rather than left. For XML attributes such as paddingLeft and layout_marginLeft, use paddingStart and layout_marginStart.

NOTE: If your minSdkVersion is less than 17, you should add both the older left/right attributes as well as the new start/right attributes. On older platforms, where RTL is not supported and the start/right attributes are unknown and therefore ignored, you need the older left/right attributes.

There is a separate lint check which catches that type of error. (Note: For Gravity#LEFT and Gravity#START, you can use these constants even when targeting older platforms, because the start bitmask is a superset of the left bitmask. Therefore, you can use gravity="start" rather than gravity="left|start".)

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-03-20 00:12

Check your image size. If you're using an unnecessarily large asset when actually deployed it might just not show despite looking correct in the designer.

查看更多
一夜七次
6楼-- · 2019-03-20 00:16

Seems like this is a bug with android, where sometimes the first image in the drawable folder doesn't show up. Added a dummy image called aaaa.png to the drawable folder and problem was solved. Found the answer here: ImageButton does not display a particular drawable

查看更多
登录 后发表回答