zero

1. BUG description

Long time no see. Teddy’s back. However, I encountered a strange BUG when I came back. To put it simply, I set the padding in XML, but it didn’t show up. No matter how large the padding was, it didn’t matter. For the first time, you had your favorite language and the best compiler you could install, and the two joys were superimposed on each other. However, why did it come to this. Let’s take a look at the code before we get dragged out and killed.

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_card"
    android:layout_width="255dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:layout_marginBottom="15dp"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:background="@drawable/bg_card_shadow"
    android:clipToPadding="false"
    android:orientation="vertical">
    ...
</LinearLayout>
Copy the code

It’s a normal code, right? Theoretically there’s 15DP padding left and right, but there’s no padding left, even though I tried padding100dp.

Two, how to solve

On Stack Overflow search, also really found the same BUG, the original click here, I will do some simple handling explanation.

The brother who encountered the problem was the same as me. He used 9patch map to set the background, and then the padding was no longer effective. This might be a bug. Two solutions were suggested:

  if(condition) {
    int bottom = theView.getPaddingBottom();
    int top = theView.getPaddingTop();
    int right = theView.getPaddingRight();
    int left = theView.getPaddingLeft();
    theView.setBackgroundResource(R.drawable.entry_bg_with_image);
    theView.setPadding(left, top, right, bottom);
  }
Copy the code

Or:

  int pad = resources.getDimensionPixelSize(R.dimen.linear_layout_padding);
  theView.setBackgroundResource(R.drawable.entry_bg_with_image);
  theView.setPadding(pad, pad, pad, pad);
Copy the code

Basically, after setting the background, you just set the padding again.

Third, how do you feel about solving this BUG

? Dare not think, this kind of BUG just remember. One takeaway is that Stack Overflow is an amazing site with almost every BUG you can think of and encounter.

// As a beginner in Android development, if I have any mistakes or shortcomings, please point them out. I hope to make progress together with you.