سؤال

I having an issue where the ImageButton background is being drawn with a different drawable from one of my resources. I'm setting the background to be transparent but on some cases it's picking up one of my drawables called bottom_shadow.9.png. Why why!? It's freaking weird...

I've seen this issue before... Some of my app users have complained seeing this issue and now I'm determined to figure this out! Take a look below what I currently have. Any tips or ideas would help.

The color value I created under values/colors.xml:

<color name="transparent">#00000000</color>

My ImageButton under my one xml layout under layout/:

<ImageButton
    android:id="@+id/ibHelp"
    android:layout_width="wrap_content"
    android:layout_height="@dimen/settings_list_item_height"
    android:background="@color/transparent"
    android:contentDescription="@string/content_desc_more_information"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:scaleType="centerInside"
    android:src="@drawable/btn_help" />

This is what I'm seeing on the generated R.java file:

public static final class drawable {
  public static final int bottom_shadow=0x7f020000;
}

public static final class color {
  public static final int transparent=0x7f080009;
}

This is what it should look like:

enter image description here

This is what I'm seeing:

enter image description here

هل كانت مفيدة؟

المحلول

Could it be related to this issue?

http://code.google.com/p/android/issues/detail?id=20283

نصائح أخرى

I think you are facing the same issue as I on another project : When using the transparent color #00000000 on a background, Android will not actually make it transparent but instead use the background drawable of the element directly under it.

Not sure what I've just said is clear but to check if this is it, I found a quick and easy solutions : Don't use #00000000 as your background transparent but any other completely transparent color : #00FF0000 or even #00F00000 should do it.

See the issue I raised in Google tracker : http://code.google.com/p/android/issues/detail?id=24653

Why are you creating your own color when it's built into Android.R.color? I would try using:

android:background="@android:color/transparent"

Whether or not it fixes your problem, it's simpler and cleaner.

I think you wanna your button's background to be some kind of color, but you have assigned both a src and a color of the button(in the layout xml), which means that the button may use the src picture as the background, not a pure color. I don't know if I made the point.

Just to add to this, I was seeing really strange periodic display corruption in my transparent ImageButton background because I was specifying the items in my background selector as follows:

<item android:drawable="@android:color/transparent" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>

This might seem to work occasionally, but I definitely had cases where the ImageButtons would render with a ghastly all-white background instead of the nice transparent background.

Note that I was mixing up the android:drawable syntax with a color resource. The correct way to specify a color resource seems to be either an android:color="#FF00FF" attribute or as a child element of item using a element. I searched long and hard, and eventually found this post.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top