java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams


java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

FrameLayout的父控件是一個LinearLayout控件,問題出在,LinearLayout為子控件分配空間的時候,獲取FrameLayout的LayoutParams的必須為LinearLayout.LayoutParams,而非FrameLayout.LayoutParams。

          簡單的舉個栗子說明一下:最外層有ReLativeLayout A,里面有兩個LinearLayout B、C,而B中又有一個一個FrameLayout D。如果要在代碼里設置B的LayoutParams,B的LayoutParams要為RelativeLayout.LayoutParams。

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);   B.setLayoutParams(params);


         而D要設置的話,需要:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0);   params.weight = 8;   D.setLayoutParams(params);



還有ListView.LayoutParams

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM