Android 在初始化时在onLayout中向CustomView添加边距
我有一个扩展FrameLayout的自定义视图:Android 在初始化时在onLayout中向CustomView添加边距,android,android-custom-view,android-framelayout,Android,Android Custom View,Android Framelayout,我有一个扩展FrameLayout的自定义视图: public SlidingDrawer(Context context, AttributeSet attrs) { this(context, attrs, 0); } public SlidingDrawer(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleA
public SlidingDrawer(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SlidingDrawer(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//get the attributes specified in attrs.xml using the name we included
TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.SlidingLayer, 0, 0);
init = true;
}
我想在customView中为布局添加一些边距:
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if(init) {
init = false;
MarginLayoutParams margins = MarginLayoutParams.class.cast(getLayoutParams());
final View parent = (View) getParent();
params.bottomMargin = mOffsetDistance - getHeight();
params.topMargin = parent.getHeight() - mOffsetDistance ;
setLayoutParams(margins);
}
}
这些利润将永远不适用。
有人能给我解释一下解决方案吗?关于
MarginLayoutParams
有什么特别的吗?顺便说一句,这些巨大的任意边距是什么?我也使用了set/getLayoutParams和同样的问题。我用正确的边距值编辑了这个问题。它们只是一个示例。只是一个瞎猜,可能是因为调用了第一个构造函数?