参考来源:/liuhaomatou/article/details/22899925#comments
1. getLayoutParams()和setLayoutParams()方法的解析
a. getLayoutParams():
/**
* 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
* , 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()}
* 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
*/
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
public getLayoutParams() {
return mLayoutParams;
}
- 1
- 2
- 3
大意就是getLayoutParams()方法的调用必须要有父控件。否则返回的就是null对象。该方法返回的是该控件的LayoutParams 对象。
b. setLayoutParams():
- 1
/**
* 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 , 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( 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();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
设置该控件的布局参数,也就是将修改好的参数信息重新赋值给该控件。
如下的示例代码:
mImageViewTabline = (ImageView) findViewById(.iv_tabline);
//
Display display = getWindow().getWindowManager().getDefaultDisplay();
DisplayMetrics displayMetrics = new DisplayMetrics();
(displayMetrics);
// 获取到屏幕的的三分之一
mScreen1_3 = / 3;
// 获取到tabline控件的LayoutParams
LayoutParams params = ();
// 设置tabline的宽度
params.width = mScreen1_3;
// 将修改好的的宽度设置给tabline控件
(params);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 关于setLayoutParams报错
在继承BaseAdapter的时候,用getView返回View的时候,用代码控制布局,需要用到,但是报错了,报的是类型转换错误,经过研究,发现,这里不能使用而必须使用对应父View的LayoutParams类型。如:某View被LinearLayout包含,则该View的setLayoutParams参数类型必须是。原因在于LinearLayout(或其他继承自ViewGroup的layout,如:RelativeLayout)在进行递归布局的时候,LinearLayout会获取子View的LayoutParams,并强制转换成,如 lp = () ();
或者是如下定义:
LayoutParams lp = (LayoutParams) ();
以转换成内部类型。
自己测试运行的时候报空指针,原因为();这里没有获得到子控件所在的布局,查看代码发现(child);应该写在上面,之后child才能getLayoutParams();
- LayoutParams类的解析
LayoutParams继承于.
LayoutParams相当于一个Layout的信息包,它封装了Layout的位置、高、宽等信息。假设在屏幕上一块区域是由一个Layout占领的,如果将一个View添加到一个Layout中,最好告诉Layout用户期望的布局方式,也就是将一个认可的layoutParams传递进去。
可以这样去形容LayoutParams,在象棋的棋盘上,每个棋子都占据一个位置,也就是每个棋子都有一个位置的信息,如这个棋子在4行4列,这里的“4行4列”就是棋子的LayoutParams。
但LayoutParams类也只是简单的描述了宽高,宽和高都可以设置成三种值:
1,一个确定的值;
2,FILL_PARENT,即填满(和父容器一样大小);
3,WRAP_CONTENT,即包裹住组件就好。
LayoutParams类是用于child view(子视图) 向 parent view(父视图)传达自己的意愿的一个东西(孩子想变成什么样向其父亲说明)其实子视图父视图可以简单理解成一个LinearLayout 和 这个LinearLayout里边一个 TextView 的关系 。TextView 就是LinearLayout的子视图 child view 。需要注意的是LayoutParams只是ViewGroup的一个内部类,这里边这个也就是ViewGroup里边这个LayoutParams类是 base class 基类实际上每个不同的ViewGroup都有自己的LayoutParams子类。
也就是说在LinearLayout类中有LayoutParams子类,在ViewGroup里面同样有LayoutParams子类。感兴趣的可以去翻看源码就知道了。
示例代码:
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
("tag", position + "," + positionOffset + "," + positionOffsetPixels);
// 获取到父控件的参数
layoutParams = () ();
// 0--->1
if (mCurrentPageIndex == 0 && position == 0) {
= (int) (positionOffset * mScreen1_3 + mCurrentPageIndex * mScreen1_3);
} else if (mCurrentPageIndex == 1 && position == 0) {
//1---->0
= (int) (mCurrentPageIndex * mScreen1_3 + (positionOffset - 1) * mScreen1_3);
} else if (mCurrentPageIndex == 1 && position == 2) {
// 1--->2
= (int) (mCurrentPageIndex * mScreen1_3 + positionOffset * mScreen1_3);
} else if (mCurrentPageIndex == 2 && position == 1) {
// 2--->1
= (int) (mCurrentPageIndex * mScreen1_3 + (positionOffset - 1) * mScreen1_3);
}
// 设置宽度给tabline
(layoutParams);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22