Nik*_*ers 8 android layoutparams
例如,如果你想要将LayoutParams某些视图更改WRAP_CONTENT为两者width和heightprogrammaticaly,它将看起来像这样:
final ViewGroup.LayoutParams lp = yourView.getLayoutParams();
lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
Run Code Online (Sandbox Code Playgroud)
因为我不知道getLayoutParams返回params的引用,所以这段代码就足够了,但我经常在例子中看到等等,在这一行之后,一个如下:
yourView.setLayoutParams(lp);
Run Code Online (Sandbox Code Playgroud)
我想知道这条线的重点是什么.这只是为了更好的代码可读性,还是有些情况下没有它就无法工作?
setLayoutParams()同时触发resolveLayoutParams()和requestLayout(),这将通知视图某些更改,然后刷新它。否则我们无法确保新的布局参数实际起作用。
查看View.setLayoutParams源代码:
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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2095 次 |
| 最近记录: |