我尝试了here发布的已接受解决方案,但似乎忽略了填充。当显示第二个视图(在这种情况下是一个按钮)时,它比具有填充的原始视图小得多。有解决方法吗?感谢
答案 0 :(得分:7)
是的,TransitionDrawable
从LayerDrawable
延伸而忽略了填充。这是基本Android代码中的getPadding()
方法,可以删除您指定的任何内容:
@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;
}
要处理它,我必须先在我的视图上设置错误的可绘制背景之前保存填充值:
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);