Gasket does not work with a specific background resource

Can someone explain to me why this is happening?

I have a pretty simple class extending TextView. When I set the background color to Color.BLUE, the indentation works fine. When I change the background resource to android.R.drawable.list_selector_background, my add-on no longer applies. What is F?

Here is my user interface class:

public class GhostDropDownOption extends TextView { TextView text_view; public GhostDropDownOption(Context context, AttributeSet attrs) { super(context, attrs); setup(context); } public GhostDropDownOption(Context context) { super(context); setup(context); } private void setup(Context context) { this.setClickable(false); // THE 2 LINES BELOW ARE THE ONLY THING I'M CHANGING //this.setBackgroundResource(android.R.drawable.list_selector_background); this.setBackgroundColor(Color.BLUE); } } 

And I use it in a layout like this:

 <trioro.voyeur.ui.GhostDropDownOption android:id="@+id/tv_dropdown_option_1" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:gravity="center_vertical" android:text="@string/request_control_dropdown_option_1" android:textColor="#000000" android:padding="10dip"/> 

And this is the result of changing the background: enter image description here

+8
android android view
source share
2 answers

Call:

 this.setBackgroundResource(android.R.drawable.list_selector_background); 

will remove any previously completed add-on (this should make it work with 9-patch assets).

Try installing an addition to the code after the line above, for example:

 this.setPadding(PADDING_CONSTANT, PADDING_CONSTANT, PADDING_CONSTANT, PADDING_CONSTANT); 

Just remember that the values ​​sent to setPadding are in NOT dip pixels!

+11
source share

You should set the wallpaper in XML, if at all possible. If you install it in code, it will use the indentation from your available resources, and not what you set in XML, so if you need to do this programmatically, you will want to get the current add-on, temporarily save it, set the background, and then indent back as @TofferJ suggests.

The reason for this is that the drawings themselves can be indented in the case of 9-patch images (where the lower and right pixel borders determine the amount of filling).

Your solution should be to simply set the background resource in XML:

android:background="@android:drawable/list_selector_background"

although I believe that this may be a private resource that you will need to copy to your project.

+2
source share

All Articles