问题
I tried the accepted solution posted here, but it appears to ignore padding. When the second view (in this case a button) displays, it's much smaller than the original which has padding. Is there a workaround for that? Thanks
回答1:
Yes, TransitionDrawable
extends from LayerDrawable
which ignores the padding. This is the getPadding()
method in the base Android code, which gets rid of anything you specified:
@Override
public boolean getPadding(Rect padding) {
// Arbitrarily get the padding from the first image.
// Technically we should maybe do something more intelligent,
// like take the max padding of all the images.
padding.left = 0;
padding.top = 0;
padding.right = 0;
padding.bottom = 0;
final ChildDrawable[] array = mLayerState.mChildren;
final int N = mLayerState.mNum;
for (int i=0; i<N; i++) {
reapplyPadding(i, array[i]);
padding.left += mPaddingL[i];
padding.top += mPaddingT[i];
padding.right += mPaddingR[i];
padding.bottom += mPaddingB[i];
}
return true;
}
See base Android code here.
To deal with it, I had to first save the padding values before setting the faulty drawable background on my view:
int bottom = theView.getPaddingBottom();
int top = theView.getPaddingTop();
int right = theView.getPaddingRight();
int left = theView.getPaddingLeft();
theView.setBackgroundResource(R.drawable.faulty_drawable);
theView.setPadding(left, top, right, bottom);
来源:https://stackoverflow.com/questions/6104677/transitiondrawable-doesnt-account-for-padding