關於Android LayoutParams的總結

記錄幾點有關LayoutParams的知識佈局

  1. LayoutParams是ViewGroup中的一個內部類,用來描述子View在ViewGroup中的位置,寬高信息。 2.其餘繼承ViewGroup的類基本上都從新寫了LayoutParams這個內部類,可是每個繼承的倒是ViewGroup中的MarginLayoutParams。
  2. 在調用view的setLayoutParams方法設置layoutParams時,其中的LayoutParams要傳其父View的LayoutParams類型,由於咱們是要在父佈局中描述view的位置及寬高佔多少。

注:關於第3點多說一點,若是你的view是寫在XML文件中,不是用代碼自動生成的,在使用setLayoutParams(LayoutParams params)方法改變view在父view中的佈局時,參數必須傳其父view的LayoutParams類型,好比父view是LinearLayout,那個就需傳LinearLayout.LayoutParams類型,不然會報錯。反之,若是view是使用代碼動態生成的,那麼就能夠傳父view的LayoutParams, 也能夠穿其父view的父類中的LayoutParams,在addView的方法內部會自動轉換,可是建議若是明確知道父view是誰,就傳父view的LayoutParams。繼承

相關文章
相關標籤/搜索