在adapter的getView中給view設定布局參數時,使用下面方法:
LayoutParams params = new LayoutParams( LayoutParams.FILL_PARENT, (int) (metrics.heightPixels * 0.23f + 0.5f)); convertView.setLayoutParams(params);
程式會出現錯誤
06-08 15:39:43.071: E/AndroidRuntime(610): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams06-08 15:39:43.071: E/AndroidRuntime(610): at android.widget.ListView.setupChild(ListView.java:1688)06-08 15:39:43.071: E/AndroidRuntime(610): at android.widget.ListView.makeAndAddView(ListView.java:1661)06-08 15:39:43.071: E/AndroidRuntime(610): at android.widget.ListView.fillDown(ListView.java:637)06-08 15:39:43.071: E/AndroidRuntime(610): at android.widget.ListView.fillFromTop(ListView.java:694)06-08 15:39:43.071: E/AndroidRuntime(610): at android.widget.ListView.layoutChildren(ListView.java:1516)06-08 15:39:43.071: E/AndroidRuntime(610): at android.widget.AbsListView.onLayout(AbsListView.java:1112)06-08 15:39:43.071: E/AndroidRuntime(610): at android.view.View.layout(View.java:6569)06-08 15:39:43.071: E/AndroidRuntime(610): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)06-08 15:39:43.071: E/AndroidRuntime(610): at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1108)06-08 15:39:43.071: E/AndroidRuntime(610): at android.widget.LinearLayout.onLayout(LinearLayout.java:920)06-08 15:39:43.071: E/AndroidRuntime(610): at android.view.View.layout(View.java:6569)06-08 15:39:43.071: E/AndroidRuntime(610): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)06-08 15:39:43.071: E/AndroidRuntime(610): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)06-08 15:39:43.071: E/AndroidRuntime(610): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)06-08 15:39:43.071: E/AndroidRuntime(610): at android.view.View.layout(View.java:6569)06-08 15:39:43.071: E/AndroidRuntime(610): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)06-08 15:39:43.071: E/AndroidRuntime(610): at android.view.View.layout(View.java:6569)06-08 15:39:43.071: E/AndroidRuntime(610): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)06-08 15:39:43.071: E/AndroidRuntime(610): at android.view.View.layout(View.java:6569)06-08 15:39:43.071: E/AndroidRuntime(610): at android.view.ViewRoot.performTraversals(ViewRoot.java:979)06-08 15:39:43.071: E/AndroidRuntime(610): at android.view.ViewRoot.handleMessage(ViewRoot.java:1613)06-08 15:39:43.071: E/AndroidRuntime(610): at android.os.Handler.dispatchMessage(Handler.java:99)06-08 15:39:43.071: E/AndroidRuntime(610): at android.os.Looper.loop(Looper.java:123)06-08 15:39:43.071: E/AndroidRuntime(610): at android.app.ActivityThread.main(ActivityThread.java:4203)06-08 15:39:43.071: E/AndroidRuntime(610): at java.lang.reflect.Method.invokeNative(Native Method)06-08 15:39:43.071: E/AndroidRuntime(610): at java.lang.reflect.Method.invoke(Method.java:521)06-08 15:39:43.071: E/AndroidRuntime(610): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)06-08 15:39:43.071: E/AndroidRuntime(610): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
但最後設定如下時:
ViewGroup.LayoutParams param = convertView.getLayoutParams(); param.height = (int) (metrics.heightPixels * 0.20f + 0.5f); convertView.setLayoutParams(param);
就OK。
http://philn.blog.163.com/blog/static/104014753201101810573338/ 裡也是一樣的情況
給LinearLayout設定LayoutParams時用初始的方法則沒有問題
暫時不知何解?