在源码里面:
LayoutParams 是ViewGroup类里面的静态内部类,
同时MarginLayoutParams extends LayoutParams 也作为ViewGroup类里面的静态内部类
然后在各个视图如:LinearLayout,RelativeLayout,AbsoluteLayout,AbsListView,ViewPager,Gallery等类里面都会去重载上面两个参数基类
而在视图View基类里面关于参数是这样的:
首先是申明:用的当然是基类,
/** * The layout parameters associated with this view and used by the parent * {@link android.view.ViewGroup} to determine how this view should be * laid out. * {@hide} */ protected ViewGroup.LayoutParams mLayoutParams;其次是get,这个大家都不陌生:
/** * Get the LayoutParams associated with this view. All views should have * layout parameters. These supply parameters to the <i>parent</i> of this * view specifying how it should be arranged. There are many subclasses of * ViewGroup.LayoutParams, and these correspond to the different subclasses * of ViewGroup that are responsible for arranging their children. * * This method may return null if this View is not attached to a parent * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)} * was not invoked successfully. When a View is attached to a parent * ViewGroup, this method must not return null. * * @return The LayoutParams associated with this view, or null if no * parameters have been set yet */ @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_") public ViewGroup.LayoutParams getLayoutParams() { return mLayoutParams; }
/** * Set the layout parameters associated with this view. These supply * parameters to the <i>parent</i> of this view specifying how it should be * arranged. There are many subclasses of ViewGroup.LayoutParams, and these * correspond to the different subclasses of ViewGroup that are responsible * for arranging their children. * * @param params The layout parameters for this view, cannot be null */ public void setLayoutParams(ViewGroup.LayoutParams params) { if (params == null) { throw new NullPointerException("Layout parameters cannot be null"); } mLayoutParams = params; resolveLayoutParams(); if (mParent instanceof ViewGroup) { ((ViewGroup) mParent).onSetLayoutParams(this, params); } requestLayout(); }确定方向:
/** * Resolve the layout parameters depending on the resolved layout direction * * @hide */ public void resolveLayoutParams() { if (mLayoutParams != null) { mLayoutParams.resolveLayoutDirection(getLayoutDirection()); } }然后在打印里面有输出当前View的参数信息,debug
平常我们调用的就是上面的get和set函数,得到LayoutParams对象,然后就可以去调用它自己的相关设置函数,主要是宽高,左右上下边距(margin),
下面四个函数是通用的:
public static void setHeightParams(View view,int dp){ LayoutParams lp=(LayoutParams)view.getLayoutParams(); lp.height=dp; view.setLayoutParams(lp); } public static void setWidthParams(View view,int dp){ LayoutParams lp=(LayoutParams)view.getLayoutParams(); lp.width=dp; view.setLayoutParams(lp); } public static void setLeftMarginParams(View view,int dp){ MarginLayoutParams lp=(MarginLayoutParams)view.getLayoutParams(); lp.leftMargin=dp; view.setLayoutParams(lp); } public static void setRightMarginParams(View view,int dp){ MarginLayoutParams lp=(MarginLayoutParams)view.getLayoutParams(); lp.rightMargin=dp; view.setLayoutParams(lp); }
但是我发现,动态的去改变这些参数好像是不行的,没有这样的函数可以调用,具体的那些参数可以动态的改变,
自己写个方法,看看提示就知道了,
/** * Per-child layout information associated with RelativeLayout. * * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignWithParentIfMissing * @attr ref android.R.styleable#RelativeLayout_Layout_layout_toLeftOf * @attr ref android.R.styleable#RelativeLayout_Layout_layout_toRightOf * @attr ref android.R.styleable#RelativeLayout_Layout_layout_above * @attr ref android.R.styleable#RelativeLayout_Layout_layout_below * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignBaseline * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignLeft * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignTop * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignRight * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignBottom * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignParentLeft * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignParentTop * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignParentRight * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignParentBottom * @attr ref android.R.styleable#RelativeLayout_Layout_layout_centerInParent * @attr ref android.R.styleable#RelativeLayout_Layout_layout_centerHorizontal * @attr ref android.R.styleable#RelativeLayout_Layout_layout_centerVertical * @attr ref android.R.styleable#RelativeLayout_Layout_layout_toStartOf * @attr ref android.R.styleable#RelativeLayout_Layout_layout_toEndOf * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignStart * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignEnd * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignParentStart * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignParentEnd */ public static class LayoutParams extends ViewGroup.MarginLayoutParams { @ViewDebug.ExportedProperty(category = "layout", resolveId = true, indexMapping = { @ViewDebug.IntToString(from = ABOVE, to = "above"), @ViewDebug.IntToString(from = ALIGN_BASELINE, to = "alignBaseline"), @ViewDebug.IntToString(from = ALIGN_BOTTOM, to = "alignBottom"), @ViewDebug.IntToString(from = ALIGN_LEFT, to = "alignLeft"), @ViewDebug.IntToString(from = ALIGN_PARENT_BOTTOM, to = "alignParentBottom"), @ViewDebug.IntToString(from = ALIGN_PARENT_LEFT, to = "alignParentLeft"), @ViewDebug.IntToString(from = ALIGN_PARENT_RIGHT, to = "alignParentRight"), @ViewDebug.IntToString(from = ALIGN_PARENT_TOP, to = "alignParentTop"), @ViewDebug.IntToString(from = ALIGN_RIGHT, to = "alignRight"), @ViewDebug.IntToString(from = ALIGN_TOP, to = "alignTop"), @ViewDebug.IntToString(from = BELOW, to = "below"), @ViewDebug.IntToString(from = CENTER_HORIZONTAL, to = "centerHorizontal"), @ViewDebug.IntToString(from = CENTER_IN_PARENT, to = "center"), @ViewDebug.IntToString(from = CENTER_VERTICAL, to = "centerVertical"), @ViewDebug.IntToString(from = LEFT_OF, to = "leftOf"), @ViewDebug.IntToString(from = RIGHT_OF, to = "rightOf"), @ViewDebug.IntToString(from = ALIGN_START, to = "alignStart"), @ViewDebug.IntToString(from = ALIGN_END, to = "alignEnd"), @ViewDebug.IntToString(from = ALIGN_PARENT_START, to = "alignParentStart"), @ViewDebug.IntToString(from = ALIGN_PARENT_END, to = "alignParentEnd"), @ViewDebug.IntToString(from = START_OF, to = "startOf"), @ViewDebug.IntToString(from = END_OF, to = "endOf") }, mapping = { @ViewDebug.IntToString(from = TRUE, to = "true"), @ViewDebug.IntToString(from = 0, to = "false/NO_ID") }) private int[] mRules = new int[VERB_COUNT]; private int[] mInitialRules = new int[VERB_COUNT]; private int mLeft, mTop, mRight, mBottom; private boolean mRulesChanged = false; private boolean mIsRtlCompatibilityMode = false;