標籤:android 使用 ar for sp on c ad r
動態建立Layout的時候,LayoutParams 是必須要設定的參數,關於 setLayoutParams 知道多少呢?
- setLayoutParams 是設給父節點的。
文檔裡說的清楚:
Set the layout parameters associated with this view. These supply parameters to the parent 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.
也就是說這個setLayoutParams()是給其父控制項看的,只有父類可以改變子View的位置布局,而不是說子View可以隨意設定。
setLayoutParams 類型不能隨便給。
清楚了LayoutParams是根據父節點來設定的,雖然使用的時候建立一個 ViewGroup.LayoutParams 也能夠設定成功,但還是要根據父節點的類型來定義為具體的 RelativeLayout.LayoutParams、LinearLayout.LayoutParams等,否則啟動並執行時候會拋出異常的。
RelativeLayout.LayoutParams layoutParams= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
view.setLayoutParams(layoutParams);
【Android】不知道的 setLayoutParams